scm.h 2.7 KB

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