scm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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.h>
  27. #include <linux/nsproxy.h>
  28. #include <linux/slab.h>
  29. #include <asm/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. /*
  37. * Only allow a user to send credentials, that they could set with
  38. * setu(g)id.
  39. */
  40. static __inline__ int scm_check_creds(struct ucred *creds)
  41. {
  42. const struct cred *cred = current_cred();
  43. kuid_t uid = make_kuid(cred->user_ns, creds->uid);
  44. kgid_t gid = make_kgid(cred->user_ns, creds->gid);
  45. if (!uid_valid(uid) || !gid_valid(gid))
  46. return -EINVAL;
  47. if ((creds->pid == task_tgid_vnr(current) || nsown_capable(CAP_SYS_ADMIN)) &&
  48. ((uid_eq(uid, cred->uid) || uid_eq(uid, cred->euid) ||
  49. uid_eq(uid, cred->suid)) || nsown_capable(CAP_SETUID)) &&
  50. ((gid_eq(gid, cred->gid) || gid_eq(gid, cred->egid) ||
  51. gid_eq(gid, cred->sgid)) || nsown_capable(CAP_SETGID))) {
  52. return 0;
  53. }
  54. return -EPERM;
  55. }
  56. static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
  57. {
  58. int *fdp = (int*)CMSG_DATA(cmsg);
  59. struct scm_fp_list *fpl = *fplp;
  60. struct file **fpp;
  61. int i, num;
  62. num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int);
  63. if (num <= 0)
  64. return 0;
  65. if (num > SCM_MAX_FD)
  66. return -EINVAL;
  67. if (!fpl)
  68. {
  69. fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
  70. if (!fpl)
  71. return -ENOMEM;
  72. *fplp = fpl;
  73. fpl->count = 0;
  74. fpl->max = SCM_MAX_FD;
  75. }
  76. fpp = &fpl->fp[fpl->count];
  77. if (fpl->count + num > fpl->max)
  78. return -EINVAL;
  79. /*
  80. * Verify the descriptors and increment the usage count.
  81. */
  82. for (i=0; i< num; i++)
  83. {
  84. int fd = fdp[i];
  85. struct file *file;
  86. if (fd < 0 || !(file = fget_raw(fd)))
  87. return -EBADF;
  88. *fpp++ = file;
  89. fpl->count++;
  90. }
  91. return num;
  92. }
  93. void __scm_destroy(struct scm_cookie *scm)
  94. {
  95. struct scm_fp_list *fpl = scm->fp;
  96. int i;
  97. if (fpl) {
  98. scm->fp = NULL;
  99. for (i=fpl->count-1; i>=0; i--)
  100. fput(fpl->fp[i]);
  101. kfree(fpl);
  102. }
  103. }
  104. EXPORT_SYMBOL(__scm_destroy);
  105. int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
  106. {
  107. struct cmsghdr *cmsg;
  108. int err;
  109. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
  110. {
  111. err = -EINVAL;
  112. /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
  113. /* The first check was omitted in <= 2.2.5. The reasoning was
  114. that parser checks cmsg_len in any case, so that
  115. additional check would be work duplication.
  116. But if cmsg_level is not SOL_SOCKET, we do not check
  117. for too short ancillary data object at all! Oops.
  118. OK, let's add it...
  119. */
  120. if (!CMSG_OK(msg, cmsg))
  121. goto error;
  122. if (cmsg->cmsg_level != SOL_SOCKET)
  123. continue;
  124. switch (cmsg->cmsg_type)
  125. {
  126. case SCM_RIGHTS:
  127. if (!sock->ops || sock->ops->family != PF_UNIX)
  128. goto error;
  129. err=scm_fp_copy(cmsg, &p->fp);
  130. if (err<0)
  131. goto error;
  132. break;
  133. case SCM_CREDENTIALS:
  134. {
  135. struct ucred creds;
  136. kuid_t uid;
  137. kgid_t gid;
  138. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
  139. goto error;
  140. memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
  141. err = scm_check_creds(&creds);
  142. if (err)
  143. goto error;
  144. p->creds.pid = creds.pid;
  145. if (!p->pid || pid_vnr(p->pid) != creds.pid) {
  146. struct pid *pid;
  147. err = -ESRCH;
  148. pid = find_get_pid(creds.pid);
  149. if (!pid)
  150. goto error;
  151. put_pid(p->pid);
  152. p->pid = pid;
  153. }
  154. err = -EINVAL;
  155. uid = make_kuid(current_user_ns(), creds.uid);
  156. gid = make_kgid(current_user_ns(), creds.gid);
  157. if (!uid_valid(uid) || !gid_valid(gid))
  158. goto error;
  159. p->creds.uid = uid;
  160. p->creds.gid = gid;
  161. if (!p->cred ||
  162. !uid_eq(p->cred->euid, uid) ||
  163. !gid_eq(p->cred->egid, gid)) {
  164. struct cred *cred;
  165. err = -ENOMEM;
  166. cred = prepare_creds();
  167. if (!cred)
  168. goto error;
  169. cred->uid = cred->euid = uid;
  170. cred->gid = cred->egid = gid;
  171. if (p->cred)
  172. put_cred(p->cred);
  173. p->cred = cred;
  174. }
  175. break;
  176. }
  177. default:
  178. goto error;
  179. }
  180. }
  181. if (p->fp && !p->fp->count)
  182. {
  183. kfree(p->fp);
  184. p->fp = NULL;
  185. }
  186. return 0;
  187. error:
  188. scm_destroy(p);
  189. return err;
  190. }
  191. EXPORT_SYMBOL(__scm_send);
  192. int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
  193. {
  194. struct cmsghdr __user *cm
  195. = (__force struct cmsghdr __user *)msg->msg_control;
  196. struct cmsghdr cmhdr;
  197. int cmlen = CMSG_LEN(len);
  198. int err;
  199. if (MSG_CMSG_COMPAT & msg->msg_flags)
  200. return put_cmsg_compat(msg, level, type, len, data);
  201. if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
  202. msg->msg_flags |= MSG_CTRUNC;
  203. return 0; /* XXX: return error? check spec. */
  204. }
  205. if (msg->msg_controllen < cmlen) {
  206. msg->msg_flags |= MSG_CTRUNC;
  207. cmlen = msg->msg_controllen;
  208. }
  209. cmhdr.cmsg_level = level;
  210. cmhdr.cmsg_type = type;
  211. cmhdr.cmsg_len = cmlen;
  212. err = -EFAULT;
  213. if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
  214. goto out;
  215. if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
  216. goto out;
  217. cmlen = CMSG_SPACE(len);
  218. if (msg->msg_controllen < cmlen)
  219. cmlen = msg->msg_controllen;
  220. msg->msg_control += cmlen;
  221. msg->msg_controllen -= cmlen;
  222. err = 0;
  223. out:
  224. return err;
  225. }
  226. EXPORT_SYMBOL(put_cmsg);
  227. void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
  228. {
  229. struct cmsghdr __user *cm
  230. = (__force struct cmsghdr __user*)msg->msg_control;
  231. int fdmax = 0;
  232. int fdnum = scm->fp->count;
  233. struct file **fp = scm->fp->fp;
  234. int __user *cmfptr;
  235. int err = 0, i;
  236. if (MSG_CMSG_COMPAT & msg->msg_flags) {
  237. scm_detach_fds_compat(msg, scm);
  238. return;
  239. }
  240. if (msg->msg_controllen > sizeof(struct cmsghdr))
  241. fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
  242. / sizeof(int));
  243. if (fdnum < fdmax)
  244. fdmax = fdnum;
  245. for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
  246. i++, cmfptr++)
  247. {
  248. struct socket *sock;
  249. int new_fd;
  250. err = security_file_receive(fp[i]);
  251. if (err)
  252. break;
  253. err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
  254. ? O_CLOEXEC : 0);
  255. if (err < 0)
  256. break;
  257. new_fd = err;
  258. err = put_user(new_fd, cmfptr);
  259. if (err) {
  260. put_unused_fd(new_fd);
  261. break;
  262. }
  263. /* Bump the usage count and install the file. */
  264. sock = sock_from_file(fp[i], &err);
  265. if (sock) {
  266. sock_update_netprioidx(sock->sk, current);
  267. sock_update_classid(sock->sk, current);
  268. }
  269. fd_install(new_fd, get_file(fp[i]));
  270. }
  271. if (i > 0)
  272. {
  273. int cmlen = CMSG_LEN(i*sizeof(int));
  274. err = put_user(SOL_SOCKET, &cm->cmsg_level);
  275. if (!err)
  276. err = put_user(SCM_RIGHTS, &cm->cmsg_type);
  277. if (!err)
  278. err = put_user(cmlen, &cm->cmsg_len);
  279. if (!err) {
  280. cmlen = CMSG_SPACE(i*sizeof(int));
  281. msg->msg_control += cmlen;
  282. msg->msg_controllen -= cmlen;
  283. }
  284. }
  285. if (i < fdnum || (fdnum && fdmax <= 0))
  286. msg->msg_flags |= MSG_CTRUNC;
  287. /*
  288. * All of the files that fit in the message have had their
  289. * usage counts incremented, so we just free the list.
  290. */
  291. __scm_destroy(scm);
  292. }
  293. EXPORT_SYMBOL(scm_detach_fds);
  294. struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
  295. {
  296. struct scm_fp_list *new_fpl;
  297. int i;
  298. if (!fpl)
  299. return NULL;
  300. new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
  301. GFP_KERNEL);
  302. if (new_fpl) {
  303. for (i = 0; i < fpl->count; i++)
  304. get_file(fpl->fp[i]);
  305. new_fpl->max = new_fpl->count;
  306. }
  307. return new_fpl;
  308. }
  309. EXPORT_SYMBOL(scm_fp_dup);