scm.c 7.8 KB

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