scm.c 7.6 KB

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