scm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 <uapi/linux/pidfd.h>
  26. #include <linux/pidfs.h>
  27. #include <linux/nsproxy.h>
  28. #include <linux/slab.h>
  29. #include <linux/errqueue.h>
  30. #include <linux/io_uring.h>
  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. #include <net/af_unix.h>
  39. /*
  40. * Only allow a user to send credentials, that they could set with
  41. * setu(g)id.
  42. */
  43. static __inline__ int scm_check_creds(struct ucred *creds)
  44. {
  45. const struct cred *cred = current_cred();
  46. kuid_t uid = make_kuid(cred->user_ns, creds->uid);
  47. kgid_t gid = make_kgid(cred->user_ns, creds->gid);
  48. if (!uid_valid(uid) || !gid_valid(gid))
  49. return -EINVAL;
  50. if ((creds->pid == task_tgid_vnr(current) ||
  51. ns_capable(task_active_pid_ns(current)->user_ns, CAP_SYS_ADMIN)) &&
  52. ((uid_eq(uid, cred->uid) || uid_eq(uid, cred->euid) ||
  53. uid_eq(uid, cred->suid)) || ns_capable(cred->user_ns, CAP_SETUID)) &&
  54. ((gid_eq(gid, cred->gid) || gid_eq(gid, cred->egid) ||
  55. gid_eq(gid, cred->sgid)) || ns_capable(cred->user_ns, CAP_SETGID))) {
  56. return 0;
  57. }
  58. return -EPERM;
  59. }
  60. static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
  61. {
  62. int *fdp = (int*)CMSG_DATA(cmsg);
  63. struct scm_fp_list *fpl = *fplp;
  64. struct file **fpp;
  65. int i, num;
  66. num = (cmsg->cmsg_len - sizeof(struct cmsghdr))/sizeof(int);
  67. if (num <= 0)
  68. return 0;
  69. if (num > SCM_MAX_FD)
  70. return -EINVAL;
  71. if (!fpl)
  72. {
  73. fpl = kmalloc_obj(struct scm_fp_list, GFP_KERNEL_ACCOUNT);
  74. if (!fpl)
  75. return -ENOMEM;
  76. *fplp = fpl;
  77. fpl->count = 0;
  78. fpl->count_unix = 0;
  79. fpl->max = SCM_MAX_FD;
  80. fpl->user = NULL;
  81. #if IS_ENABLED(CONFIG_UNIX)
  82. fpl->inflight = false;
  83. fpl->dead = false;
  84. fpl->edges = NULL;
  85. INIT_LIST_HEAD(&fpl->vertices);
  86. #endif
  87. }
  88. fpp = &fpl->fp[fpl->count];
  89. if (fpl->count + num > fpl->max)
  90. return -EINVAL;
  91. /*
  92. * Verify the descriptors and increment the usage count.
  93. */
  94. for (i=0; i< num; i++)
  95. {
  96. int fd = fdp[i];
  97. struct file *file;
  98. if (fd < 0 || !(file = fget_raw(fd)))
  99. return -EBADF;
  100. /* don't allow io_uring files */
  101. if (io_is_uring_fops(file)) {
  102. fput(file);
  103. return -EINVAL;
  104. }
  105. if (unix_get_socket(file))
  106. fpl->count_unix++;
  107. *fpp++ = file;
  108. fpl->count++;
  109. }
  110. if (!fpl->user)
  111. fpl->user = get_uid(current_user());
  112. return num;
  113. }
  114. void __scm_destroy(struct scm_cookie *scm)
  115. {
  116. struct scm_fp_list *fpl = scm->fp;
  117. int i;
  118. if (fpl) {
  119. scm->fp = NULL;
  120. for (i=fpl->count-1; i>=0; i--)
  121. fput(fpl->fp[i]);
  122. free_uid(fpl->user);
  123. kfree(fpl);
  124. }
  125. }
  126. EXPORT_SYMBOL(__scm_destroy);
  127. static inline int scm_replace_pid(struct scm_cookie *scm, struct pid *pid)
  128. {
  129. int err;
  130. /* drop all previous references */
  131. scm_destroy_cred(scm);
  132. err = pidfs_register_pid(pid);
  133. if (unlikely(err))
  134. return err;
  135. scm->pid = pid;
  136. scm->creds.pid = pid_vnr(pid);
  137. return 0;
  138. }
  139. int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
  140. {
  141. const struct proto_ops *ops = READ_ONCE(sock->ops);
  142. struct cmsghdr *cmsg;
  143. int err;
  144. for_each_cmsghdr(cmsg, msg) {
  145. err = -EINVAL;
  146. /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
  147. /* The first check was omitted in <= 2.2.5. The reasoning was
  148. that parser checks cmsg_len in any case, so that
  149. additional check would be work duplication.
  150. But if cmsg_level is not SOL_SOCKET, we do not check
  151. for too short ancillary data object at all! Oops.
  152. OK, let's add it...
  153. */
  154. if (!CMSG_OK(msg, cmsg))
  155. goto error;
  156. if (cmsg->cmsg_level != SOL_SOCKET)
  157. continue;
  158. switch (cmsg->cmsg_type)
  159. {
  160. case SCM_RIGHTS:
  161. if (!ops || ops->family != PF_UNIX)
  162. goto error;
  163. err=scm_fp_copy(cmsg, &p->fp);
  164. if (err<0)
  165. goto error;
  166. break;
  167. case SCM_CREDENTIALS:
  168. {
  169. struct ucred creds;
  170. kuid_t uid;
  171. kgid_t gid;
  172. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
  173. goto error;
  174. memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
  175. err = scm_check_creds(&creds);
  176. if (err)
  177. goto error;
  178. if (!p->pid || pid_vnr(p->pid) != creds.pid) {
  179. struct pid *pid;
  180. err = -ESRCH;
  181. pid = find_get_pid(creds.pid);
  182. if (!pid)
  183. goto error;
  184. /* pass a struct pid reference from
  185. * find_get_pid() to scm_replace_pid().
  186. */
  187. err = scm_replace_pid(p, pid);
  188. if (err) {
  189. put_pid(pid);
  190. goto error;
  191. }
  192. }
  193. err = -EINVAL;
  194. uid = make_kuid(current_user_ns(), creds.uid);
  195. gid = make_kgid(current_user_ns(), creds.gid);
  196. if (!uid_valid(uid) || !gid_valid(gid))
  197. goto error;
  198. p->creds.uid = uid;
  199. p->creds.gid = gid;
  200. break;
  201. }
  202. default:
  203. goto error;
  204. }
  205. }
  206. if (p->fp && !p->fp->count)
  207. {
  208. kfree(p->fp);
  209. p->fp = NULL;
  210. }
  211. return 0;
  212. error:
  213. scm_destroy(p);
  214. return err;
  215. }
  216. EXPORT_SYMBOL(__scm_send);
  217. int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
  218. {
  219. int cmlen = CMSG_LEN(len);
  220. if (msg->msg_flags & MSG_CMSG_COMPAT)
  221. return put_cmsg_compat(msg, level, type, len, data);
  222. if (!msg->msg_control || msg->msg_controllen < sizeof(struct cmsghdr)) {
  223. msg->msg_flags |= MSG_CTRUNC;
  224. return 0; /* XXX: return error? check spec. */
  225. }
  226. if (msg->msg_controllen < cmlen) {
  227. msg->msg_flags |= MSG_CTRUNC;
  228. cmlen = msg->msg_controllen;
  229. }
  230. if (msg->msg_control_is_user) {
  231. struct cmsghdr __user *cm = msg->msg_control_user;
  232. check_object_size(data, cmlen - sizeof(*cm), true);
  233. scoped_user_write_access_size(cm, cmlen, efault) {
  234. unsafe_put_user(cmlen, &cm->cmsg_len, efault);
  235. unsafe_put_user(level, &cm->cmsg_level, efault);
  236. unsafe_put_user(type, &cm->cmsg_type, efault);
  237. unsafe_copy_to_user(CMSG_USER_DATA(cm), data,
  238. cmlen - sizeof(*cm), efault);
  239. }
  240. } else {
  241. struct cmsghdr *cm = msg->msg_control;
  242. cm->cmsg_level = level;
  243. cm->cmsg_type = type;
  244. cm->cmsg_len = cmlen;
  245. memcpy(CMSG_DATA(cm), data, cmlen - sizeof(*cm));
  246. }
  247. cmlen = min(CMSG_SPACE(len), msg->msg_controllen);
  248. if (msg->msg_control_is_user)
  249. msg->msg_control_user += cmlen;
  250. else
  251. msg->msg_control += cmlen;
  252. msg->msg_controllen -= cmlen;
  253. return 0;
  254. efault:
  255. return -EFAULT;
  256. }
  257. EXPORT_SYMBOL(put_cmsg);
  258. int put_cmsg_notrunc(struct msghdr *msg, int level, int type, int len,
  259. void *data)
  260. {
  261. /* Don't produce truncated CMSGs */
  262. if (!msg->msg_control || msg->msg_controllen < CMSG_LEN(len))
  263. return -ETOOSMALL;
  264. return put_cmsg(msg, level, type, len, data);
  265. }
  266. void put_cmsg_scm_timestamping64(struct msghdr *msg, struct scm_timestamping_internal *tss_internal)
  267. {
  268. struct scm_timestamping64 tss;
  269. int i;
  270. for (i = 0; i < ARRAY_SIZE(tss.ts); i++) {
  271. tss.ts[i].tv_sec = tss_internal->ts[i].tv_sec;
  272. tss.ts[i].tv_nsec = tss_internal->ts[i].tv_nsec;
  273. }
  274. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_NEW, sizeof(tss), &tss);
  275. }
  276. EXPORT_SYMBOL(put_cmsg_scm_timestamping64);
  277. void put_cmsg_scm_timestamping(struct msghdr *msg, struct scm_timestamping_internal *tss_internal)
  278. {
  279. struct scm_timestamping tss;
  280. int i;
  281. for (i = 0; i < ARRAY_SIZE(tss.ts); i++) {
  282. tss.ts[i].tv_sec = tss_internal->ts[i].tv_sec;
  283. tss.ts[i].tv_nsec = tss_internal->ts[i].tv_nsec;
  284. }
  285. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_OLD, sizeof(tss), &tss);
  286. }
  287. EXPORT_SYMBOL(put_cmsg_scm_timestamping);
  288. static int scm_max_fds(struct msghdr *msg)
  289. {
  290. if (msg->msg_controllen <= sizeof(struct cmsghdr))
  291. return 0;
  292. return (msg->msg_controllen - sizeof(struct cmsghdr)) / sizeof(int);
  293. }
  294. void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
  295. {
  296. struct cmsghdr __user *cm =
  297. (__force struct cmsghdr __user *)msg->msg_control_user;
  298. unsigned int o_flags = (msg->msg_flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0;
  299. int fdmax = min_t(int, scm_max_fds(msg), scm->fp->count);
  300. int __user *cmsg_data = CMSG_USER_DATA(cm);
  301. int err = 0, i;
  302. /* no use for FD passing from kernel space callers */
  303. if (WARN_ON_ONCE(!msg->msg_control_is_user))
  304. return;
  305. if (msg->msg_flags & MSG_CMSG_COMPAT) {
  306. scm_detach_fds_compat(msg, scm);
  307. return;
  308. }
  309. for (i = 0; i < fdmax; i++) {
  310. err = scm_recv_one_fd(scm->fp->fp[i], cmsg_data + i, o_flags);
  311. if (err < 0)
  312. break;
  313. }
  314. if (i > 0) {
  315. int cmlen = CMSG_LEN(i * sizeof(int));
  316. err = put_user(SOL_SOCKET, &cm->cmsg_level);
  317. if (!err)
  318. err = put_user(SCM_RIGHTS, &cm->cmsg_type);
  319. if (!err)
  320. err = put_user(cmlen, &cm->cmsg_len);
  321. if (!err) {
  322. cmlen = CMSG_SPACE(i * sizeof(int));
  323. if (msg->msg_controllen < cmlen)
  324. cmlen = msg->msg_controllen;
  325. msg->msg_control_user += cmlen;
  326. msg->msg_controllen -= cmlen;
  327. }
  328. }
  329. if (i < scm->fp->count || (scm->fp->count && fdmax <= 0))
  330. msg->msg_flags |= MSG_CTRUNC;
  331. /*
  332. * All of the files that fit in the message have had their usage counts
  333. * incremented, so we just free the list.
  334. */
  335. __scm_destroy(scm);
  336. }
  337. EXPORT_SYMBOL(scm_detach_fds);
  338. struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
  339. {
  340. struct scm_fp_list *new_fpl;
  341. int i;
  342. if (!fpl)
  343. return NULL;
  344. new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
  345. GFP_KERNEL_ACCOUNT);
  346. if (new_fpl) {
  347. for (i = 0; i < fpl->count; i++)
  348. get_file(fpl->fp[i]);
  349. new_fpl->max = new_fpl->count;
  350. new_fpl->user = get_uid(fpl->user);
  351. #if IS_ENABLED(CONFIG_UNIX)
  352. new_fpl->inflight = false;
  353. new_fpl->edges = NULL;
  354. INIT_LIST_HEAD(&new_fpl->vertices);
  355. #endif
  356. }
  357. return new_fpl;
  358. }
  359. EXPORT_SYMBOL(scm_fp_dup);
  360. #ifdef CONFIG_SECURITY_NETWORK
  361. static void scm_passec(struct sock *sk, struct msghdr *msg, struct scm_cookie *scm)
  362. {
  363. struct lsm_context ctx;
  364. int err;
  365. if (sk->sk_scm_security) {
  366. err = security_secid_to_secctx(scm->secid, &ctx);
  367. if (err >= 0) {
  368. put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, ctx.len,
  369. ctx.context);
  370. security_release_secctx(&ctx);
  371. }
  372. }
  373. }
  374. static bool scm_has_secdata(struct sock *sk)
  375. {
  376. return sk->sk_scm_security;
  377. }
  378. #else
  379. static void scm_passec(struct sock *sk, struct msghdr *msg, struct scm_cookie *scm)
  380. {
  381. }
  382. static bool scm_has_secdata(struct sock *sk)
  383. {
  384. return false;
  385. }
  386. #endif
  387. static void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm)
  388. {
  389. struct file *pidfd_file = NULL;
  390. int len, pidfd;
  391. /* put_cmsg() doesn't return an error if CMSG is truncated,
  392. * that's why we need to opencode these checks here.
  393. */
  394. if (msg->msg_flags & MSG_CMSG_COMPAT)
  395. len = sizeof(struct compat_cmsghdr) + sizeof(int);
  396. else
  397. len = sizeof(struct cmsghdr) + sizeof(int);
  398. if (msg->msg_controllen < len) {
  399. msg->msg_flags |= MSG_CTRUNC;
  400. return;
  401. }
  402. if (!scm->pid)
  403. return;
  404. pidfd = pidfd_prepare(scm->pid, PIDFD_STALE, &pidfd_file);
  405. if (put_cmsg(msg, SOL_SOCKET, SCM_PIDFD, sizeof(int), &pidfd)) {
  406. if (pidfd_file) {
  407. put_unused_fd(pidfd);
  408. fput(pidfd_file);
  409. }
  410. return;
  411. }
  412. if (pidfd_file)
  413. fd_install(pidfd, pidfd_file);
  414. }
  415. static bool __scm_recv_common(struct sock *sk, struct msghdr *msg,
  416. struct scm_cookie *scm, int flags)
  417. {
  418. if (!msg->msg_control) {
  419. if (sk->sk_scm_credentials || sk->sk_scm_pidfd ||
  420. scm->fp || scm_has_secdata(sk))
  421. msg->msg_flags |= MSG_CTRUNC;
  422. scm_destroy(scm);
  423. return false;
  424. }
  425. if (sk->sk_scm_credentials) {
  426. struct user_namespace *current_ns = current_user_ns();
  427. struct ucred ucreds = {
  428. .pid = scm->creds.pid,
  429. .uid = from_kuid_munged(current_ns, scm->creds.uid),
  430. .gid = from_kgid_munged(current_ns, scm->creds.gid),
  431. };
  432. put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(ucreds), &ucreds);
  433. }
  434. scm_passec(sk, msg, scm);
  435. if (scm->fp)
  436. scm_detach_fds(msg, scm);
  437. return true;
  438. }
  439. void scm_recv(struct socket *sock, struct msghdr *msg,
  440. struct scm_cookie *scm, int flags)
  441. {
  442. if (!__scm_recv_common(sock->sk, msg, scm, flags))
  443. return;
  444. scm_destroy_cred(scm);
  445. }
  446. EXPORT_SYMBOL(scm_recv);
  447. void scm_recv_unix(struct socket *sock, struct msghdr *msg,
  448. struct scm_cookie *scm, int flags)
  449. {
  450. if (!__scm_recv_common(sock->sk, msg, scm, flags))
  451. return;
  452. if (sock->sk->sk_scm_pidfd)
  453. scm_pidfd_recv(msg, scm);
  454. scm_destroy_cred(scm);
  455. }