scm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/string.h>
  5. #include <linux/socket.h>
  6. #include <linux/net.h>
  7. #include <linux/fs.h>
  8. #include <net/af_unix.h>
  9. #include <net/scm.h>
  10. #include <linux/init.h>
  11. #include <linux/io_uring.h>
  12. #include "scm.h"
  13. unsigned int unix_tot_inflight;
  14. EXPORT_SYMBOL(unix_tot_inflight);
  15. LIST_HEAD(gc_inflight_list);
  16. EXPORT_SYMBOL(gc_inflight_list);
  17. DEFINE_SPINLOCK(unix_gc_lock);
  18. EXPORT_SYMBOL(unix_gc_lock);
  19. struct sock *unix_get_socket(struct file *filp)
  20. {
  21. struct sock *u_sock = NULL;
  22. struct inode *inode = file_inode(filp);
  23. /* Socket ? */
  24. if (S_ISSOCK(inode->i_mode) && !(filp->f_mode & FMODE_PATH)) {
  25. struct socket *sock = SOCKET_I(inode);
  26. struct sock *s = sock->sk;
  27. /* PF_UNIX ? */
  28. if (s && sock->ops && sock->ops->family == PF_UNIX)
  29. u_sock = s;
  30. } else {
  31. /* Could be an io_uring instance */
  32. u_sock = io_uring_get_socket(filp);
  33. }
  34. return u_sock;
  35. }
  36. EXPORT_SYMBOL(unix_get_socket);
  37. /* Keep the number of times in flight count for the file
  38. * descriptor if it is for an AF_UNIX socket.
  39. */
  40. void unix_inflight(struct user_struct *user, struct file *fp)
  41. {
  42. struct sock *s = unix_get_socket(fp);
  43. spin_lock(&unix_gc_lock);
  44. if (s) {
  45. struct unix_sock *u = unix_sk(s);
  46. if (!u->inflight) {
  47. BUG_ON(!list_empty(&u->link));
  48. list_add_tail(&u->link, &gc_inflight_list);
  49. } else {
  50. BUG_ON(list_empty(&u->link));
  51. }
  52. u->inflight++;
  53. /* Paired with READ_ONCE() in wait_for_unix_gc() */
  54. WRITE_ONCE(unix_tot_inflight, unix_tot_inflight + 1);
  55. }
  56. WRITE_ONCE(user->unix_inflight, user->unix_inflight + 1);
  57. spin_unlock(&unix_gc_lock);
  58. }
  59. void unix_notinflight(struct user_struct *user, struct file *fp)
  60. {
  61. struct sock *s = unix_get_socket(fp);
  62. spin_lock(&unix_gc_lock);
  63. if (s) {
  64. struct unix_sock *u = unix_sk(s);
  65. BUG_ON(!u->inflight);
  66. BUG_ON(list_empty(&u->link));
  67. u->inflight--;
  68. if (!u->inflight)
  69. list_del_init(&u->link);
  70. /* Paired with READ_ONCE() in wait_for_unix_gc() */
  71. WRITE_ONCE(unix_tot_inflight, unix_tot_inflight - 1);
  72. }
  73. WRITE_ONCE(user->unix_inflight, user->unix_inflight - 1);
  74. spin_unlock(&unix_gc_lock);
  75. }
  76. /*
  77. * The "user->unix_inflight" variable is protected by the garbage
  78. * collection lock, and we just read it locklessly here. If you go
  79. * over the limit, there might be a tiny race in actually noticing
  80. * it across threads. Tough.
  81. */
  82. static inline bool too_many_unix_fds(struct task_struct *p)
  83. {
  84. struct user_struct *user = current_user();
  85. if (unlikely(READ_ONCE(user->unix_inflight) > task_rlimit(p, RLIMIT_NOFILE)))
  86. return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
  87. return false;
  88. }
  89. int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
  90. {
  91. int i;
  92. if (too_many_unix_fds(current))
  93. return -ETOOMANYREFS;
  94. /*
  95. * Need to duplicate file references for the sake of garbage
  96. * collection. Otherwise a socket in the fps might become a
  97. * candidate for GC while the skb is not yet queued.
  98. */
  99. UNIXCB(skb).fp = scm_fp_dup(scm->fp);
  100. if (!UNIXCB(skb).fp)
  101. return -ENOMEM;
  102. for (i = scm->fp->count - 1; i >= 0; i--)
  103. unix_inflight(scm->fp->user, scm->fp->fp[i]);
  104. return 0;
  105. }
  106. EXPORT_SYMBOL(unix_attach_fds);
  107. void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
  108. {
  109. int i;
  110. scm->fp = UNIXCB(skb).fp;
  111. UNIXCB(skb).fp = NULL;
  112. for (i = scm->fp->count-1; i >= 0; i--)
  113. unix_notinflight(scm->fp->user, scm->fp->fp[i]);
  114. }
  115. EXPORT_SYMBOL(unix_detach_fds);
  116. void unix_destruct_scm(struct sk_buff *skb)
  117. {
  118. struct scm_cookie scm;
  119. memset(&scm, 0, sizeof(scm));
  120. scm.pid = UNIXCB(skb).pid;
  121. if (UNIXCB(skb).fp)
  122. unix_detach_fds(&scm, skb);
  123. /* Alas, it calls VFS */
  124. /* So fscking what? fput() had been SMP-safe since the last Summer */
  125. scm_destroy(&scm);
  126. sock_wfree(skb);
  127. }
  128. EXPORT_SYMBOL(unix_destruct_scm);