scm.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* scm.c - Socket level control messages processing.
  3. *
  4. * Author: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  5. * Alignment and value checking mods by Craig Metz
  6. */
  7. #include <linux/module.h>
  8. #include <linux/signal.h>
  9. #include <linux/capability.h>
  10. #include <linux/errno.h>
  11. #include <linux/sched.h>
  12. #include <linux/sched/user.h>
  13. #include <linux/mm.h>
  14. #include <linux/kernel.h>
  15. #include <linux/stat.h>
  16. #include <linux/socket.h>
  17. #include <linux/file.h>
  18. #include <linux/fcntl.h>
  19. #include <linux/net.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/security.h>
  23. #include <linux/pid_namespace.h>
  24. #include <linux/pid.h>
  25. #include <linux/nsproxy.h>
  26. #include <linux/slab.h>
  27. #include <linux/errqueue.h>
  28. #include <linux/io_uring.h>
  29. #include <linux/uaccess.h>
  30. #include <net/protocol.h>
  31. #include <linux/skbuff.h>
  32. #include <net/sock.h>
  33. #include <net/compat.h>
  34. #include <net/scm.h>
  35. #include <net/cls_cgroup.h>
  36. #include <net/af_unix.h>
  37. /*
  38. * Only allow a user to send credentials, that they could set with
  39. * setu(g)id.
  40. */
  41. static __inline__ int scm_check_creds(struct ucred *creds)
  42. {
  43. const struct cred *cred = current_cred();
  44. kuid_t uid = make_kuid(cred->user_ns, creds->uid);
  45. kgid_t gid = make_kgid(cred->user_ns, creds->gid);
  46. if (!uid_valid(uid) || !gid_valid(gid))
  47. return -EINVAL;
  48. if ((creds->pid == task_tgid_vnr(current) ||
  49. ns_capable(task_active_pid_ns(current)->user_ns, CAP_SYS_ADMIN)) &&
  50. ((uid_eq(uid, cred->uid) || uid_eq(uid, cred->euid) ||
  51. uid_eq(uid, cred->suid)) || ns_capable(cred->user_ns, CAP_SETUID)) &&
  52. ((gid_eq(gid, cred->gid) || gid_eq(gid, cred->egid) ||
  53. gid_eq(gid, cred->sgid)) || ns_capable(cred->user_ns, CAP_SETGID))) {
  54. return 0;
  55. }
  56. return -EPERM;
  57. }
  58. static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
  59. {
  60. int *fdp = (int*)CMSG_DATA(cmsg);
  61. struct scm_fp_list *fpl = *fplp;
  62. struct file **fpp;
  63. int i, num;
  64. num = (cmsg->cmsg_len - sizeof(struct cmsghdr))/sizeof(int);
  65. if (num <= 0)
  66. return 0;
  67. if (num > SCM_MAX_FD)
  68. return -EINVAL;
  69. if (!fpl)
  70. {
  71. fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL_ACCOUNT);
  72. if (!fpl)
  73. return -ENOMEM;
  74. *fplp = fpl;
  75. fpl->count = 0;
  76. fpl->count_unix = 0;
  77. fpl->max = SCM_MAX_FD;
  78. fpl->user = NULL;
  79. #if IS_ENABLED(CONFIG_UNIX)
  80. fpl->inflight = false;
  81. fpl->dead = false;
  82. fpl->edges = NULL;
  83. INIT_LIST_HEAD(&fpl->vertices);
  84. #endif
  85. }
  86. fpp = &fpl->fp[fpl->count];
  87. if (fpl->count + num > fpl->max)
  88. return -EINVAL;
  89. /*
  90. * Verify the descriptors and increment the usage count.
  91. */
  92. for (i=0; i< num; i++)
  93. {
  94. int fd = fdp[i];
  95. struct file *file;
  96. if (fd < 0 || !(file = fget_raw(fd)))
  97. return -EBADF;
  98. /* don't allow io_uring files */
  99. if (io_is_uring_fops(file)) {
  100. fput(file);
  101. return -EINVAL;
  102. }
  103. if (unix_get_socket(file))
  104. fpl->count_unix++;
  105. *fpp++ = file;
  106. fpl->count++;
  107. }
  108. if (!fpl->user)
  109. fpl->user = get_uid(current_user());
  110. return num;
  111. }
  112. void __scm_destroy(struct scm_cookie *scm)
  113. {
  114. struct scm_fp_list *fpl = scm->fp;
  115. int i;
  116. if (fpl) {
  117. scm->fp = NULL;
  118. for (i=fpl->count-1; i>=0; i--)
  119. fput(fpl->fp[i]);
  120. free_uid(fpl->user);
  121. kfree(fpl);
  122. }
  123. }
  124. EXPORT_SYMBOL(__scm_destroy);
  125. int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
  126. {
  127. const struct proto_ops *ops = READ_ONCE(sock->ops);
  128. struct cmsghdr *cmsg;
  129. int err;
  130. for_each_cmsghdr(cmsg, msg) {
  131. err = -EINVAL;
  132. /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
  133. /* The first check was omitted in <= 2.2.5. The reasoning was
  134. that parser checks cmsg_len in any case, so that
  135. additional check would be work duplication.
  136. But if cmsg_level is not SOL_SOCKET, we do not check
  137. for too short ancillary data object at all! Oops.
  138. OK, let's add it...
  139. */
  140. if (!CMSG_OK(msg, cmsg))
  141. goto error;
  142. if (cmsg->cmsg_level != SOL_SOCKET)
  143. continue;
  144. switch (cmsg->cmsg_type)
  145. {
  146. case SCM_RIGHTS:
  147. if (!ops || ops->family != PF_UNIX)
  148. goto error;
  149. err=scm_fp_copy(cmsg, &p->fp);
  150. if (err<0)
  151. goto error;
  152. break;
  153. case SCM_CREDENTIALS:
  154. {
  155. struct ucred creds;
  156. kuid_t uid;
  157. kgid_t gid;
  158. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
  159. goto error;
  160. memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
  161. err = scm_check_creds(&creds);
  162. if (err)
  163. goto error;
  164. p->creds.pid = creds.pid;
  165. if (!p->pid || pid_vnr(p->pid) != creds.pid) {
  166. struct pid *pid;
  167. err = -ESRCH;
  168. pid = find_get_pid(creds.pid);
  169. if (!pid)
  170. goto error;
  171. put_pid(p->pid);
  172. p->pid = pid;
  173. }
  174. err = -EINVAL;
  175. uid = make_kuid(current_user_ns(), creds.uid);
  176. gid = make_kgid(current_user_ns(), creds.gid);
  177. if (!uid_valid(uid) || !gid_valid(gid))
  178. goto error;
  179. p->creds.uid = uid;
  180. p->creds.gid = gid;
  181. break;
  182. }
  183. default:
  184. goto error;
  185. }
  186. }
  187. if (p->fp && !p->fp->count)
  188. {
  189. kfree(p->fp);
  190. p->fp = NULL;
  191. }
  192. return 0;
  193. error:
  194. scm_destroy(p);
  195. return err;
  196. }
  197. EXPORT_SYMBOL(__scm_send);
  198. int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
  199. {
  200. int cmlen = CMSG_LEN(len);
  201. if (msg->msg_flags & MSG_CMSG_COMPAT)
  202. return put_cmsg_compat(msg, level, type, len, data);
  203. if (!msg->msg_control || msg->msg_controllen < sizeof(struct cmsghdr)) {
  204. msg->msg_flags |= MSG_CTRUNC;
  205. return 0; /* XXX: return error? check spec. */
  206. }
  207. if (msg->msg_controllen < cmlen) {
  208. msg->msg_flags |= MSG_CTRUNC;
  209. cmlen = msg->msg_controllen;
  210. }
  211. if (msg->msg_control_is_user) {
  212. struct cmsghdr __user *cm = msg->msg_control_user;
  213. check_object_size(data, cmlen - sizeof(*cm), true);
  214. if (!user_write_access_begin(cm, cmlen))
  215. goto efault;
  216. unsafe_put_user(cmlen, &cm->cmsg_len, efault_end);
  217. unsafe_put_user(level, &cm->cmsg_level, efault_end);
  218. unsafe_put_user(type, &cm->cmsg_type, efault_end);
  219. unsafe_copy_to_user(CMSG_USER_DATA(cm), data,
  220. cmlen - sizeof(*cm), efault_end);
  221. user_write_access_end();
  222. } else {
  223. struct cmsghdr *cm = msg->msg_control;
  224. cm->cmsg_level = level;
  225. cm->cmsg_type = type;
  226. cm->cmsg_len = cmlen;
  227. memcpy(CMSG_DATA(cm), data, cmlen - sizeof(*cm));
  228. }
  229. cmlen = min(CMSG_SPACE(len), msg->msg_controllen);
  230. if (msg->msg_control_is_user)
  231. msg->msg_control_user += cmlen;
  232. else
  233. msg->msg_control += cmlen;
  234. msg->msg_controllen -= cmlen;
  235. return 0;
  236. efault_end:
  237. user_write_access_end();
  238. efault:
  239. return -EFAULT;
  240. }
  241. EXPORT_SYMBOL(put_cmsg);
  242. int put_cmsg_notrunc(struct msghdr *msg, int level, int type, int len,
  243. void *data)
  244. {
  245. /* Don't produce truncated CMSGs */
  246. if (!msg->msg_control || msg->msg_controllen < CMSG_LEN(len))
  247. return -ETOOSMALL;
  248. return put_cmsg(msg, level, type, len, data);
  249. }
  250. void put_cmsg_scm_timestamping64(struct msghdr *msg, struct scm_timestamping_internal *tss_internal)
  251. {
  252. struct scm_timestamping64 tss;
  253. int i;
  254. for (i = 0; i < ARRAY_SIZE(tss.ts); i++) {
  255. tss.ts[i].tv_sec = tss_internal->ts[i].tv_sec;
  256. tss.ts[i].tv_nsec = tss_internal->ts[i].tv_nsec;
  257. }
  258. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_NEW, sizeof(tss), &tss);
  259. }
  260. EXPORT_SYMBOL(put_cmsg_scm_timestamping64);
  261. void put_cmsg_scm_timestamping(struct msghdr *msg, struct scm_timestamping_internal *tss_internal)
  262. {
  263. struct scm_timestamping tss;
  264. int i;
  265. for (i = 0; i < ARRAY_SIZE(tss.ts); i++) {
  266. tss.ts[i].tv_sec = tss_internal->ts[i].tv_sec;
  267. tss.ts[i].tv_nsec = tss_internal->ts[i].tv_nsec;
  268. }
  269. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_OLD, sizeof(tss), &tss);
  270. }
  271. EXPORT_SYMBOL(put_cmsg_scm_timestamping);
  272. static int scm_max_fds(struct msghdr *msg)
  273. {
  274. if (msg->msg_controllen <= sizeof(struct cmsghdr))
  275. return 0;
  276. return (msg->msg_controllen - sizeof(struct cmsghdr)) / sizeof(int);
  277. }
  278. void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
  279. {
  280. struct cmsghdr __user *cm =
  281. (__force struct cmsghdr __user *)msg->msg_control_user;
  282. unsigned int o_flags = (msg->msg_flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0;
  283. int fdmax = min_t(int, scm_max_fds(msg), scm->fp->count);
  284. int __user *cmsg_data = CMSG_USER_DATA(cm);
  285. int err = 0, i;
  286. /* no use for FD passing from kernel space callers */
  287. if (WARN_ON_ONCE(!msg->msg_control_is_user))
  288. return;
  289. if (msg->msg_flags & MSG_CMSG_COMPAT) {
  290. scm_detach_fds_compat(msg, scm);
  291. return;
  292. }
  293. for (i = 0; i < fdmax; i++) {
  294. err = scm_recv_one_fd(scm->fp->fp[i], cmsg_data + i, o_flags);
  295. if (err < 0)
  296. break;
  297. }
  298. if (i > 0) {
  299. int cmlen = CMSG_LEN(i * sizeof(int));
  300. err = put_user(SOL_SOCKET, &cm->cmsg_level);
  301. if (!err)
  302. err = put_user(SCM_RIGHTS, &cm->cmsg_type);
  303. if (!err)
  304. err = put_user(cmlen, &cm->cmsg_len);
  305. if (!err) {
  306. cmlen = CMSG_SPACE(i * sizeof(int));
  307. if (msg->msg_controllen < cmlen)
  308. cmlen = msg->msg_controllen;
  309. msg->msg_control_user += cmlen;
  310. msg->msg_controllen -= cmlen;
  311. }
  312. }
  313. if (i < scm->fp->count || (scm->fp->count && fdmax <= 0))
  314. msg->msg_flags |= MSG_CTRUNC;
  315. /*
  316. * All of the files that fit in the message have had their usage counts
  317. * incremented, so we just free the list.
  318. */
  319. __scm_destroy(scm);
  320. }
  321. EXPORT_SYMBOL(scm_detach_fds);
  322. struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
  323. {
  324. struct scm_fp_list *new_fpl;
  325. int i;
  326. if (!fpl)
  327. return NULL;
  328. new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
  329. GFP_KERNEL_ACCOUNT);
  330. if (new_fpl) {
  331. for (i = 0; i < fpl->count; i++)
  332. get_file(fpl->fp[i]);
  333. new_fpl->max = new_fpl->count;
  334. new_fpl->user = get_uid(fpl->user);
  335. #if IS_ENABLED(CONFIG_UNIX)
  336. new_fpl->inflight = false;
  337. new_fpl->edges = NULL;
  338. INIT_LIST_HEAD(&new_fpl->vertices);
  339. #endif
  340. }
  341. return new_fpl;
  342. }
  343. EXPORT_SYMBOL(scm_fp_dup);