scm.c 6.6 KB

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