scm.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/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. const 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. fpl->max = SCM_MAX_FD;
  70. fpl->user = NULL;
  71. }
  72. fpp = &fpl->fp[fpl->count];
  73. if (fpl->count + num > fpl->max)
  74. return -EINVAL;
  75. /*
  76. * Verify the descriptors and increment the usage count.
  77. */
  78. for (i=0; i< num; i++)
  79. {
  80. int fd = fdp[i];
  81. struct file *file;
  82. if (fd < 0 || !(file = fget_raw(fd)))
  83. return -EBADF;
  84. *fpp++ = file;
  85. fpl->count++;
  86. }
  87. if (!fpl->user)
  88. fpl->user = get_uid(current_user());
  89. return num;
  90. }
  91. void __scm_destroy(struct scm_cookie *scm)
  92. {
  93. struct scm_fp_list *fpl = scm->fp;
  94. int i;
  95. if (fpl) {
  96. scm->fp = NULL;
  97. if (current->scm_work_list) {
  98. list_add_tail(&fpl->list, current->scm_work_list);
  99. } else {
  100. LIST_HEAD(work_list);
  101. current->scm_work_list = &work_list;
  102. list_add(&fpl->list, &work_list);
  103. while (!list_empty(&work_list)) {
  104. fpl = list_first_entry(&work_list, struct scm_fp_list, list);
  105. list_del(&fpl->list);
  106. for (i=fpl->count-1; i>=0; i--)
  107. fput(fpl->fp[i]);
  108. free_uid(fpl->user);
  109. kfree(fpl);
  110. }
  111. current->scm_work_list = NULL;
  112. }
  113. }
  114. }
  115. EXPORT_SYMBOL(__scm_destroy);
  116. int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
  117. {
  118. struct cmsghdr *cmsg;
  119. int err;
  120. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
  121. {
  122. err = -EINVAL;
  123. /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
  124. /* The first check was omitted in <= 2.2.5. The reasoning was
  125. that parser checks cmsg_len in any case, so that
  126. additional check would be work duplication.
  127. But if cmsg_level is not SOL_SOCKET, we do not check
  128. for too short ancillary data object at all! Oops.
  129. OK, let's add it...
  130. */
  131. if (!CMSG_OK(msg, cmsg))
  132. goto error;
  133. if (cmsg->cmsg_level != SOL_SOCKET)
  134. continue;
  135. switch (cmsg->cmsg_type)
  136. {
  137. case SCM_RIGHTS:
  138. if (!sock->ops || sock->ops->family != PF_UNIX)
  139. goto error;
  140. err=scm_fp_copy(cmsg, &p->fp);
  141. if (err<0)
  142. goto error;
  143. break;
  144. case SCM_CREDENTIALS:
  145. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
  146. goto error;
  147. memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred));
  148. err = scm_check_creds(&p->creds);
  149. if (err)
  150. goto error;
  151. if (!p->pid || pid_vnr(p->pid) != p->creds.pid) {
  152. struct pid *pid;
  153. err = -ESRCH;
  154. pid = find_get_pid(p->creds.pid);
  155. if (!pid)
  156. goto error;
  157. put_pid(p->pid);
  158. p->pid = pid;
  159. }
  160. if (!p->cred ||
  161. (p->cred->euid != p->creds.uid) ||
  162. (p->cred->egid != p->creds.gid)) {
  163. struct cred *cred;
  164. err = -ENOMEM;
  165. cred = prepare_creds();
  166. if (!cred)
  167. goto error;
  168. cred->uid = cred->euid = p->creds.uid;
  169. cred->gid = cred->egid = p->creds.gid;
  170. if (p->cred)
  171. put_cred(p->cred);
  172. p->cred = cred;
  173. }
  174. break;
  175. default:
  176. goto error;
  177. }
  178. }
  179. if (p->fp && !p->fp->count)
  180. {
  181. kfree(p->fp);
  182. p->fp = NULL;
  183. }
  184. return 0;
  185. error:
  186. scm_destroy(p);
  187. return err;
  188. }
  189. EXPORT_SYMBOL(__scm_send);
  190. int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
  191. {
  192. struct cmsghdr __user *cm
  193. = (__force struct cmsghdr __user *)msg->msg_control;
  194. struct cmsghdr cmhdr;
  195. int cmlen = CMSG_LEN(len);
  196. int err;
  197. if (MSG_CMSG_COMPAT & msg->msg_flags)
  198. return put_cmsg_compat(msg, level, type, len, data);
  199. if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
  200. msg->msg_flags |= MSG_CTRUNC;
  201. return 0; /* XXX: return error? check spec. */
  202. }
  203. if (msg->msg_controllen < cmlen) {
  204. msg->msg_flags |= MSG_CTRUNC;
  205. cmlen = msg->msg_controllen;
  206. }
  207. cmhdr.cmsg_level = level;
  208. cmhdr.cmsg_type = type;
  209. cmhdr.cmsg_len = cmlen;
  210. err = -EFAULT;
  211. if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
  212. goto out;
  213. if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
  214. goto out;
  215. cmlen = CMSG_SPACE(len);
  216. if (msg->msg_controllen < cmlen)
  217. cmlen = msg->msg_controllen;
  218. msg->msg_control += cmlen;
  219. msg->msg_controllen -= cmlen;
  220. err = 0;
  221. out:
  222. return err;
  223. }
  224. EXPORT_SYMBOL(put_cmsg);
  225. void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
  226. {
  227. struct cmsghdr __user *cm
  228. = (__force struct cmsghdr __user*)msg->msg_control;
  229. int fdmax = 0;
  230. int fdnum = scm->fp->count;
  231. struct file **fp = scm->fp->fp;
  232. int __user *cmfptr;
  233. int err = 0, i;
  234. if (MSG_CMSG_COMPAT & msg->msg_flags) {
  235. scm_detach_fds_compat(msg, scm);
  236. return;
  237. }
  238. if (msg->msg_controllen > sizeof(struct cmsghdr))
  239. fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
  240. / sizeof(int));
  241. if (fdnum < fdmax)
  242. fdmax = fdnum;
  243. for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
  244. i++, cmfptr++)
  245. {
  246. int new_fd;
  247. err = security_file_receive(fp[i]);
  248. if (err)
  249. break;
  250. err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
  251. ? O_CLOEXEC : 0);
  252. if (err < 0)
  253. break;
  254. new_fd = err;
  255. err = put_user(new_fd, cmfptr);
  256. if (err) {
  257. put_unused_fd(new_fd);
  258. break;
  259. }
  260. /* Bump the usage count and install the file. */
  261. fd_install(new_fd, get_file(fp[i]));
  262. }
  263. if (i > 0)
  264. {
  265. int cmlen = CMSG_LEN(i*sizeof(int));
  266. err = put_user(SOL_SOCKET, &cm->cmsg_level);
  267. if (!err)
  268. err = put_user(SCM_RIGHTS, &cm->cmsg_type);
  269. if (!err)
  270. err = put_user(cmlen, &cm->cmsg_len);
  271. if (!err) {
  272. cmlen = CMSG_SPACE(i*sizeof(int));
  273. msg->msg_control += cmlen;
  274. msg->msg_controllen -= cmlen;
  275. }
  276. }
  277. if (i < fdnum || (fdnum && fdmax <= 0))
  278. msg->msg_flags |= MSG_CTRUNC;
  279. /*
  280. * All of the files that fit in the message have had their
  281. * usage counts incremented, so we just free the list.
  282. */
  283. __scm_destroy(scm);
  284. }
  285. EXPORT_SYMBOL(scm_detach_fds);
  286. struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
  287. {
  288. struct scm_fp_list *new_fpl;
  289. int i;
  290. if (!fpl)
  291. return NULL;
  292. new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
  293. GFP_KERNEL);
  294. if (new_fpl) {
  295. for (i = 0; i < fpl->count; i++)
  296. get_file(fpl->fp[i]);
  297. new_fpl->max = new_fpl->count;
  298. new_fpl->user = get_uid(fpl->user);
  299. }
  300. return new_fpl;
  301. }
  302. EXPORT_SYMBOL(scm_fp_dup);