scm.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 253
  12. struct scm_fp_list {
  13. struct list_head list;
  14. short count;
  15. short max;
  16. struct file *fp[SCM_MAX_FD];
  17. };
  18. struct scm_cookie {
  19. struct pid *pid; /* Skb credentials */
  20. const struct cred *cred;
  21. struct scm_fp_list *fp; /* Passed files */
  22. struct ucred creds; /* Skb credentials */
  23. #ifdef CONFIG_SECURITY_NETWORK
  24. u32 secid; /* Passed security ID */
  25. #endif
  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_set_cred(struct scm_cookie *scm,
  42. struct pid *pid, const struct cred *cred)
  43. {
  44. scm->pid = get_pid(pid);
  45. scm->cred = get_cred(cred);
  46. cred_to_ucred(pid, cred, &scm->creds);
  47. }
  48. static __inline__ void scm_set_cred_noref(struct scm_cookie *scm,
  49. struct pid *pid, const struct cred *cred)
  50. {
  51. scm->pid = pid;
  52. scm->cred = cred;
  53. cred_to_ucred(pid, cred, &scm->creds);
  54. }
  55. static __inline__ void scm_destroy_cred(struct scm_cookie *scm)
  56. {
  57. put_pid(scm->pid);
  58. scm->pid = NULL;
  59. if (scm->cred)
  60. put_cred(scm->cred);
  61. scm->cred = NULL;
  62. }
  63. static __inline__ void scm_destroy(struct scm_cookie *scm)
  64. {
  65. scm_destroy_cred(scm);
  66. if (scm && scm->fp)
  67. __scm_destroy(scm);
  68. }
  69. static __inline__ void scm_release(struct scm_cookie *scm)
  70. {
  71. /* keep ref on pid and cred */
  72. scm->pid = NULL;
  73. scm->cred = NULL;
  74. if (scm->fp)
  75. __scm_destroy(scm);
  76. }
  77. static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,
  78. struct scm_cookie *scm)
  79. {
  80. scm_set_cred(scm, task_tgid(current), current_cred());
  81. scm->fp = NULL;
  82. unix_get_peersec_dgram(sock, scm);
  83. if (msg->msg_controllen <= 0)
  84. return 0;
  85. return __scm_send(sock, msg, scm);
  86. }
  87. #ifdef CONFIG_SECURITY_NETWORK
  88. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  89. {
  90. char *secdata;
  91. u32 seclen;
  92. int err;
  93. if (test_bit(SOCK_PASSSEC, &sock->flags)) {
  94. err = security_secid_to_secctx(scm->secid, &secdata, &seclen);
  95. if (!err) {
  96. put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, seclen, secdata);
  97. security_release_secctx(secdata, seclen);
  98. }
  99. }
  100. }
  101. #else
  102. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  103. { }
  104. #endif /* CONFIG_SECURITY_NETWORK */
  105. static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg,
  106. struct scm_cookie *scm, int flags)
  107. {
  108. if (!msg->msg_control) {
  109. if (test_bit(SOCK_PASSCRED, &sock->flags) || scm->fp)
  110. msg->msg_flags |= MSG_CTRUNC;
  111. if (scm && scm->fp)
  112. __scm_destroy(scm);
  113. return;
  114. }
  115. if (test_bit(SOCK_PASSCRED, &sock->flags))
  116. put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(scm->creds), &scm->creds);
  117. scm_passec(sock, msg, scm);
  118. if (!scm->fp)
  119. return;
  120. scm_detach_fds(msg, scm);
  121. }
  122. #endif /* __LINUX_NET_SCM_H */