scm.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 user_struct *user;
  17. struct file *fp[SCM_MAX_FD];
  18. };
  19. struct scm_cookie {
  20. struct pid *pid; /* Skb credentials */
  21. const struct cred *cred;
  22. struct scm_fp_list *fp; /* Passed files */
  23. struct ucred creds; /* Skb credentials */
  24. #ifdef CONFIG_SECURITY_NETWORK
  25. u32 secid; /* Passed security ID */
  26. #endif
  27. };
  28. extern void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm);
  29. extern void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm);
  30. extern int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm);
  31. extern void __scm_destroy(struct scm_cookie *scm);
  32. extern struct scm_fp_list * scm_fp_dup(struct scm_fp_list *fpl);
  33. #ifdef CONFIG_SECURITY_NETWORK
  34. static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
  35. {
  36. security_socket_getpeersec_dgram(sock, NULL, &scm->secid);
  37. }
  38. #else
  39. static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
  40. { }
  41. #endif /* CONFIG_SECURITY_NETWORK */
  42. static __inline__ void scm_set_cred(struct scm_cookie *scm,
  43. struct pid *pid, const struct cred *cred)
  44. {
  45. scm->pid = get_pid(pid);
  46. scm->cred = cred ? get_cred(cred) : NULL;
  47. cred_to_ucred(pid, cred, &scm->creds, false);
  48. }
  49. static __inline__ void scm_destroy_cred(struct scm_cookie *scm)
  50. {
  51. put_pid(scm->pid);
  52. scm->pid = NULL;
  53. if (scm->cred)
  54. put_cred(scm->cred);
  55. scm->cred = NULL;
  56. }
  57. static __inline__ void scm_destroy(struct scm_cookie *scm)
  58. {
  59. scm_destroy_cred(scm);
  60. if (scm && scm->fp)
  61. __scm_destroy(scm);
  62. }
  63. static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,
  64. struct scm_cookie *scm, bool forcecreds)
  65. {
  66. memset(scm, 0, sizeof(*scm));
  67. if (forcecreds)
  68. scm_set_cred(scm, task_tgid(current), current_cred());
  69. unix_get_peersec_dgram(sock, scm);
  70. if (msg->msg_controllen <= 0)
  71. return 0;
  72. return __scm_send(sock, msg, scm);
  73. }
  74. #ifdef CONFIG_SECURITY_NETWORK
  75. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  76. {
  77. char *secdata;
  78. u32 seclen;
  79. int err;
  80. if (test_bit(SOCK_PASSSEC, &sock->flags)) {
  81. err = security_secid_to_secctx(scm->secid, &secdata, &seclen);
  82. if (!err) {
  83. put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, seclen, secdata);
  84. security_release_secctx(secdata, seclen);
  85. }
  86. }
  87. }
  88. #else
  89. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  90. { }
  91. #endif /* CONFIG_SECURITY_NETWORK */
  92. static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg,
  93. struct scm_cookie *scm, int flags)
  94. {
  95. if (!msg->msg_control) {
  96. if (test_bit(SOCK_PASSCRED, &sock->flags) || scm->fp)
  97. msg->msg_flags |= MSG_CTRUNC;
  98. scm_destroy(scm);
  99. return;
  100. }
  101. if (test_bit(SOCK_PASSCRED, &sock->flags))
  102. put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(scm->creds), &scm->creds);
  103. scm_destroy_cred(scm);
  104. scm_passec(sock, msg, scm);
  105. if (!scm->fp)
  106. return;
  107. scm_detach_fds(msg, scm);
  108. }
  109. #endif /* __LINUX_NET_SCM_H */