scm.c 6.4 KB

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