scm.c 6.3 KB

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