scm.c 6.9 KB

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