/kern_oII/net/core/scm.c

http://omnia2droid.googlecode.com/ · C · 316 lines · 241 code · 45 blank · 30 comment · 61 complexity · 1c710747d304cc82f3bbe6685905ae23 MD5 · raw file

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