scm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* scm.c - Socket level control messages processing.
  2. *
  3. * Author: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  4. * Alignment and value checking mods by Craig Metz
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/signal.h>
  13. #include <linux/capability.h>
  14. #include <linux/errno.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/user.h>
  17. #include <linux/mm.h>
  18. #include <linux/kernel.h>
  19. #include <linux/stat.h>
  20. #include <linux/socket.h>
  21. #include <linux/file.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/net.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/security.h>
  27. #include <linux/pid_namespace.h>
  28. #include <linux/pid.h>
  29. #include <linux/nsproxy.h>
  30. #include <linux/slab.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. /*
  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);
  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. *fpp++ = file;
  93. fpl->count++;
  94. }
  95. if (!fpl->user)
  96. fpl->user = get_uid(current_user());
  97. return num;
  98. }
  99. void __scm_destroy(struct scm_cookie *scm)
  100. {
  101. struct scm_fp_list *fpl = scm->fp;
  102. int i;
  103. if (fpl) {
  104. scm->fp = NULL;
  105. for (i=fpl->count-1; i>=0; i--)
  106. fput(fpl->fp[i]);
  107. free_uid(fpl->user);
  108. kfree(fpl);
  109. }
  110. }
  111. EXPORT_SYMBOL(__scm_destroy);
  112. int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
  113. {
  114. struct cmsghdr *cmsg;
  115. int err;
  116. for_each_cmsghdr(cmsg, msg) {
  117. err = -EINVAL;
  118. /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
  119. /* The first check was omitted in <= 2.2.5. The reasoning was
  120. that parser checks cmsg_len in any case, so that
  121. additional check would be work duplication.
  122. But if cmsg_level is not SOL_SOCKET, we do not check
  123. for too short ancillary data object at all! Oops.
  124. OK, let's add it...
  125. */
  126. if (!CMSG_OK(msg, cmsg))
  127. goto error;
  128. if (cmsg->cmsg_level != SOL_SOCKET)
  129. continue;
  130. switch (cmsg->cmsg_type)
  131. {
  132. case SCM_RIGHTS:
  133. if (!sock->ops || sock->ops->family != PF_UNIX)
  134. goto error;
  135. err=scm_fp_copy(cmsg, &p->fp);
  136. if (err<0)
  137. goto error;
  138. break;
  139. case SCM_CREDENTIALS:
  140. {
  141. struct ucred creds;
  142. kuid_t uid;
  143. kgid_t gid;
  144. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
  145. goto error;
  146. memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
  147. err = scm_check_creds(&creds);
  148. if (err)
  149. goto error;
  150. p->creds.pid = creds.pid;
  151. if (!p->pid || pid_vnr(p->pid) != creds.pid) {
  152. struct pid *pid;
  153. err = -ESRCH;
  154. pid = find_get_pid(creds.pid);
  155. if (!pid)
  156. goto error;
  157. put_pid(p->pid);
  158. p->pid = pid;
  159. }
  160. err = -EINVAL;
  161. uid = make_kuid(current_user_ns(), creds.uid);
  162. gid = make_kgid(current_user_ns(), creds.gid);
  163. if (!uid_valid(uid) || !gid_valid(gid))
  164. goto error;
  165. p->creds.uid = uid;
  166. p->creds.gid = gid;
  167. break;
  168. }
  169. default:
  170. goto error;
  171. }
  172. }
  173. if (p->fp && !p->fp->count)
  174. {
  175. kfree(p->fp);
  176. p->fp = NULL;
  177. }
  178. return 0;
  179. error:
  180. scm_destroy(p);
  181. return err;
  182. }
  183. EXPORT_SYMBOL(__scm_send);
  184. int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
  185. {
  186. struct cmsghdr __user *cm
  187. = (__force struct cmsghdr __user *)msg->msg_control;
  188. struct cmsghdr cmhdr;
  189. int cmlen = CMSG_LEN(len);
  190. int err;
  191. if (MSG_CMSG_COMPAT & msg->msg_flags)
  192. return put_cmsg_compat(msg, level, type, len, data);
  193. if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
  194. msg->msg_flags |= MSG_CTRUNC;
  195. return 0; /* XXX: return error? check spec. */
  196. }
  197. if (msg->msg_controllen < cmlen) {
  198. msg->msg_flags |= MSG_CTRUNC;
  199. cmlen = msg->msg_controllen;
  200. }
  201. cmhdr.cmsg_level = level;
  202. cmhdr.cmsg_type = type;
  203. cmhdr.cmsg_len = cmlen;
  204. err = -EFAULT;
  205. if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
  206. goto out;
  207. if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
  208. goto out;
  209. cmlen = CMSG_SPACE(len);
  210. if (msg->msg_controllen < cmlen)
  211. cmlen = msg->msg_controllen;
  212. msg->msg_control += cmlen;
  213. msg->msg_controllen -= cmlen;
  214. err = 0;
  215. out:
  216. return err;
  217. }
  218. EXPORT_SYMBOL(put_cmsg);
  219. void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
  220. {
  221. struct cmsghdr __user *cm
  222. = (__force struct cmsghdr __user*)msg->msg_control;
  223. int fdmax = 0;
  224. int fdnum = scm->fp->count;
  225. struct file **fp = scm->fp->fp;
  226. int __user *cmfptr;
  227. int err = 0, i;
  228. if (MSG_CMSG_COMPAT & msg->msg_flags) {
  229. scm_detach_fds_compat(msg, scm);
  230. return;
  231. }
  232. if (msg->msg_controllen > sizeof(struct cmsghdr))
  233. fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
  234. / sizeof(int));
  235. if (fdnum < fdmax)
  236. fdmax = fdnum;
  237. for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
  238. i++, cmfptr++)
  239. {
  240. struct socket *sock;
  241. int new_fd;
  242. err = security_file_receive(fp[i]);
  243. if (err)
  244. break;
  245. err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
  246. ? O_CLOEXEC : 0);
  247. if (err < 0)
  248. break;
  249. new_fd = err;
  250. err = put_user(new_fd, cmfptr);
  251. if (err) {
  252. put_unused_fd(new_fd);
  253. break;
  254. }
  255. /* Bump the usage count and install the file. */
  256. sock = sock_from_file(fp[i], &err);
  257. if (sock) {
  258. sock_update_netprioidx(&sock->sk->sk_cgrp_data);
  259. sock_update_classid(&sock->sk->sk_cgrp_data);
  260. }
  261. fd_install(new_fd, get_file(fp[i]));
  262. }
  263. if (i > 0)
  264. {
  265. int cmlen = CMSG_LEN(i*sizeof(int));
  266. err = put_user(SOL_SOCKET, &cm->cmsg_level);
  267. if (!err)
  268. err = put_user(SCM_RIGHTS, &cm->cmsg_type);
  269. if (!err)
  270. err = put_user(cmlen, &cm->cmsg_len);
  271. if (!err) {
  272. cmlen = CMSG_SPACE(i*sizeof(int));
  273. if (msg->msg_controllen < cmlen)
  274. cmlen = msg->msg_controllen;
  275. msg->msg_control += cmlen;
  276. msg->msg_controllen -= cmlen;
  277. }
  278. }
  279. if (i < fdnum || (fdnum && fdmax <= 0))
  280. msg->msg_flags |= MSG_CTRUNC;
  281. /*
  282. * All of the files that fit in the message have had their
  283. * usage counts incremented, so we just free the list.
  284. */
  285. __scm_destroy(scm);
  286. }
  287. EXPORT_SYMBOL(scm_detach_fds);
  288. struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
  289. {
  290. struct scm_fp_list *new_fpl;
  291. int i;
  292. if (!fpl)
  293. return NULL;
  294. new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
  295. GFP_KERNEL);
  296. if (new_fpl) {
  297. for (i = 0; i < fpl->count; i++)
  298. get_file(fpl->fp[i]);
  299. new_fpl->max = new_fpl->count;
  300. new_fpl->user = get_uid(fpl->user);
  301. }
  302. return new_fpl;
  303. }
  304. EXPORT_SYMBOL(scm_fp_dup);