scm.c 8.7 KB

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