scm.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef __LINUX_NET_SCM_H
  2. #define __LINUX_NET_SCM_H
  3. #include <linux/limits.h>
  4. #include <linux/net.h>
  5. #include <linux/security.h>
  6. #include <linux/pid.h>
  7. #include <linux/nsproxy.h>
  8. /* Well, we should have at least one descriptor open
  9. * to accept passed FDs 8)
  10. */
  11. #define SCM_MAX_FD 255
  12. struct scm_fp_list
  13. {
  14. struct list_head list;
  15. int count;
  16. struct file *fp[SCM_MAX_FD];
  17. };
  18. struct scm_cookie
  19. {
  20. struct ucred creds; /* Skb credentials */
  21. struct scm_fp_list *fp; /* Passed files */
  22. #ifdef CONFIG_SECURITY_NETWORK
  23. u32 secid; /* Passed security ID */
  24. #endif
  25. unsigned long seq; /* Connection seqno */
  26. };
  27. extern void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm);
  28. extern void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm);
  29. extern int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm);
  30. extern void __scm_destroy(struct scm_cookie *scm);
  31. extern struct scm_fp_list * scm_fp_dup(struct scm_fp_list *fpl);
  32. #ifdef CONFIG_SECURITY_NETWORK
  33. static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
  34. {
  35. security_socket_getpeersec_dgram(sock, NULL, &scm->secid);
  36. }
  37. #else
  38. static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
  39. { }
  40. #endif /* CONFIG_SECURITY_NETWORK */
  41. static __inline__ void scm_destroy(struct scm_cookie *scm)
  42. {
  43. if (scm && scm->fp)
  44. __scm_destroy(scm);
  45. }
  46. static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,
  47. struct scm_cookie *scm)
  48. {
  49. struct task_struct *p = current;
  50. scm->creds.uid = current_uid();
  51. scm->creds.gid = current_gid();
  52. scm->creds.pid = task_tgid_vnr(p);
  53. scm->fp = NULL;
  54. scm->seq = 0;
  55. unix_get_peersec_dgram(sock, scm);
  56. if (msg->msg_controllen <= 0)
  57. return 0;
  58. return __scm_send(sock, msg, scm);
  59. }
  60. #ifdef CONFIG_SECURITY_NETWORK
  61. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  62. {
  63. char *secdata;
  64. u32 seclen;
  65. int err;
  66. if (test_bit(SOCK_PASSSEC, &sock->flags)) {
  67. err = security_secid_to_secctx(scm->secid, &secdata, &seclen);
  68. if (!err) {
  69. put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, seclen, secdata);
  70. security_release_secctx(secdata, seclen);
  71. }
  72. }
  73. }
  74. #else
  75. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  76. { }
  77. #endif /* CONFIG_SECURITY_NETWORK */
  78. static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg,
  79. struct scm_cookie *scm, int flags)
  80. {
  81. if (!msg->msg_control)
  82. {
  83. if (test_bit(SOCK_PASSCRED, &sock->flags) || scm->fp)
  84. msg->msg_flags |= MSG_CTRUNC;
  85. scm_destroy(scm);
  86. return;
  87. }
  88. if (test_bit(SOCK_PASSCRED, &sock->flags))
  89. put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(scm->creds), &scm->creds);
  90. scm_passec(sock, msg, scm);
  91. if (!scm->fp)
  92. return;
  93. scm_detach_fds(msg, scm);
  94. }
  95. #endif /* __LINUX_NET_SCM_H */