scm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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/system.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. /*
  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. if ((creds->pid == task_tgid_vnr(current) || capable(CAP_SYS_ADMIN)) &&
  44. ((creds->uid == cred->uid || creds->uid == cred->euid ||
  45. creds->uid == cred->suid) || capable(CAP_SETUID)) &&
  46. ((creds->gid == cred->gid || creds->gid == cred->egid ||
  47. creds->gid == cred->sgid) || capable(CAP_SETGID))) {
  48. return 0;
  49. }
  50. return -EPERM;
  51. }
  52. static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
  53. {
  54. int *fdp = (int*)CMSG_DATA(cmsg);
  55. struct scm_fp_list *fpl = *fplp;
  56. struct file **fpp;
  57. int i, num;
  58. num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int);
  59. if (num <= 0)
  60. return 0;
  61. if (num > SCM_MAX_FD)
  62. return -EINVAL;
  63. if (!fpl)
  64. {
  65. fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
  66. if (!fpl)
  67. return -ENOMEM;
  68. *fplp = fpl;
  69. fpl->count = 0;
  70. }
  71. fpp = &fpl->fp[fpl->count];
  72. if (fpl->count + num > SCM_MAX_FD)
  73. return -EINVAL;
  74. /*
  75. * Verify the descriptors and increment the usage count.
  76. */
  77. for (i=0; i< num; i++)
  78. {
  79. int fd = fdp[i];
  80. struct file *file;
  81. if (fd < 0 || !(file = fget(fd)))
  82. return -EBADF;
  83. *fpp++ = file;
  84. fpl->count++;
  85. }
  86. return num;
  87. }
  88. void __scm_destroy(struct scm_cookie *scm)
  89. {
  90. struct scm_fp_list *fpl = scm->fp;
  91. int i;
  92. if (fpl) {
  93. scm->fp = NULL;
  94. if (current->scm_work_list) {
  95. list_add_tail(&fpl->list, current->scm_work_list);
  96. } else {
  97. LIST_HEAD(work_list);
  98. current->scm_work_list = &work_list;
  99. list_add(&fpl->list, &work_list);
  100. while (!list_empty(&work_list)) {
  101. fpl = list_first_entry(&work_list, struct scm_fp_list, list);
  102. list_del(&fpl->list);
  103. for (i=fpl->count-1; i>=0; i--)
  104. fput(fpl->fp[i]);
  105. kfree(fpl);
  106. }
  107. current->scm_work_list = NULL;
  108. }
  109. }
  110. }
  111. int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
  112. {
  113. struct cmsghdr *cmsg;
  114. int err;
  115. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
  116. {
  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. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
  141. goto error;
  142. memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred));
  143. err = scm_check_creds(&p->creds);
  144. if (err)
  145. goto error;
  146. if (pid_vnr(p->pid) != p->creds.pid) {
  147. struct pid *pid;
  148. err = -ESRCH;
  149. pid = find_get_pid(p->creds.pid);
  150. if (!pid)
  151. goto error;
  152. put_pid(p->pid);
  153. p->pid = pid;
  154. }
  155. if ((p->cred->euid != p->creds.uid) ||
  156. (p->cred->egid != p->creds.gid)) {
  157. struct cred *cred;
  158. err = -ENOMEM;
  159. cred = prepare_creds();
  160. if (!cred)
  161. goto error;
  162. cred->uid = cred->euid = p->creds.uid;
  163. cred->gid = cred->egid = p->creds.uid;
  164. put_cred(p->cred);
  165. p->cred = cred;
  166. }
  167. break;
  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. int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
  183. {
  184. struct cmsghdr __user *cm
  185. = (__force struct cmsghdr __user *)msg->msg_control;
  186. struct cmsghdr cmhdr;
  187. int cmlen = CMSG_LEN(len);
  188. int err;
  189. if (MSG_CMSG_COMPAT & msg->msg_flags)
  190. return put_cmsg_compat(msg, level, type, len, data);
  191. if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
  192. msg->msg_flags |= MSG_CTRUNC;
  193. return 0; /* XXX: return error? check spec. */
  194. }
  195. if (msg->msg_controllen < cmlen) {
  196. msg->msg_flags |= MSG_CTRUNC;
  197. cmlen = msg->msg_controllen;
  198. }
  199. cmhdr.cmsg_level = level;
  200. cmhdr.cmsg_type = type;
  201. cmhdr.cmsg_len = cmlen;
  202. err = -EFAULT;
  203. if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
  204. goto out;
  205. if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
  206. goto out;
  207. cmlen = CMSG_SPACE(len);
  208. if (msg->msg_controllen < cmlen)
  209. cmlen = msg->msg_controllen;
  210. msg->msg_control += cmlen;
  211. msg->msg_controllen -= cmlen;
  212. err = 0;
  213. out:
  214. return err;
  215. }
  216. void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
  217. {
  218. struct cmsghdr __user *cm
  219. = (__force struct cmsghdr __user*)msg->msg_control;
  220. int fdmax = 0;
  221. int fdnum = scm->fp->count;
  222. struct file **fp = scm->fp->fp;
  223. int __user *cmfptr;
  224. int err = 0, i;
  225. if (MSG_CMSG_COMPAT & msg->msg_flags) {
  226. scm_detach_fds_compat(msg, scm);
  227. return;
  228. }
  229. if (msg->msg_controllen > sizeof(struct cmsghdr))
  230. fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
  231. / sizeof(int));
  232. if (fdnum < fdmax)
  233. fdmax = fdnum;
  234. for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
  235. i++, cmfptr++)
  236. {
  237. int new_fd;
  238. err = security_file_receive(fp[i]);
  239. if (err)
  240. break;
  241. err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
  242. ? O_CLOEXEC : 0);
  243. if (err < 0)
  244. break;
  245. new_fd = err;
  246. err = put_user(new_fd, cmfptr);
  247. if (err) {
  248. put_unused_fd(new_fd);
  249. break;
  250. }
  251. /* Bump the usage count and install the file. */
  252. get_file(fp[i]);
  253. fd_install(new_fd, fp[i]);
  254. }
  255. if (i > 0)
  256. {
  257. int cmlen = CMSG_LEN(i*sizeof(int));
  258. err = put_user(SOL_SOCKET, &cm->cmsg_level);
  259. if (!err)
  260. err = put_user(SCM_RIGHTS, &cm->cmsg_type);
  261. if (!err)
  262. err = put_user(cmlen, &cm->cmsg_len);
  263. if (!err) {
  264. cmlen = CMSG_SPACE(i*sizeof(int));
  265. msg->msg_control += cmlen;
  266. msg->msg_controllen -= cmlen;
  267. }
  268. }
  269. if (i < fdnum || (fdnum && fdmax <= 0))
  270. msg->msg_flags |= MSG_CTRUNC;
  271. /*
  272. * All of the files that fit in the message have had their
  273. * usage counts incremented, so we just free the list.
  274. */
  275. __scm_destroy(scm);
  276. }
  277. struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
  278. {
  279. struct scm_fp_list *new_fpl;
  280. int i;
  281. if (!fpl)
  282. return NULL;
  283. new_fpl = kmalloc(sizeof(*fpl), GFP_KERNEL);
  284. if (new_fpl) {
  285. for (i=fpl->count-1; i>=0; i--)
  286. get_file(fpl->fp[i]);
  287. memcpy(new_fpl, fpl, sizeof(*fpl));
  288. }
  289. return new_fpl;
  290. }
  291. EXPORT_SYMBOL(__scm_destroy);
  292. EXPORT_SYMBOL(__scm_send);
  293. EXPORT_SYMBOL(put_cmsg);
  294. EXPORT_SYMBOL(scm_detach_fds);
  295. EXPORT_SYMBOL(scm_fp_dup);