bpf_fs_kfuncs.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2024 Google LLC. */
  3. #include <linux/bpf.h>
  4. #include <linux/btf.h>
  5. #include <linux/btf_ids.h>
  6. #include <linux/dcache.h>
  7. #include <linux/fs.h>
  8. #include <linux/file.h>
  9. #include <linux/mm.h>
  10. #include <linux/xattr.h>
  11. __bpf_kfunc_start_defs();
  12. /**
  13. * bpf_get_task_exe_file - get a reference on the exe_file struct file member of
  14. * the mm_struct that is nested within the supplied
  15. * task_struct
  16. * @task: task_struct of which the nested mm_struct exe_file member to get a
  17. * reference on
  18. *
  19. * Get a reference on the exe_file struct file member field of the mm_struct
  20. * nested within the supplied *task*. The referenced file pointer acquired by
  21. * this BPF kfunc must be released using bpf_put_file(). Failing to call
  22. * bpf_put_file() on the returned referenced struct file pointer that has been
  23. * acquired by this BPF kfunc will result in the BPF program being rejected by
  24. * the BPF verifier.
  25. *
  26. * This BPF kfunc may only be called from BPF LSM programs.
  27. *
  28. * Internally, this BPF kfunc leans on get_task_exe_file(), such that calling
  29. * bpf_get_task_exe_file() would be analogous to calling get_task_exe_file()
  30. * directly in kernel context.
  31. *
  32. * Return: A referenced struct file pointer to the exe_file member of the
  33. * mm_struct that is nested within the supplied *task*. On error, NULL is
  34. * returned.
  35. */
  36. __bpf_kfunc struct file *bpf_get_task_exe_file(struct task_struct *task)
  37. {
  38. return get_task_exe_file(task);
  39. }
  40. /**
  41. * bpf_put_file - put a reference on the supplied file
  42. * @file: file to put a reference on
  43. *
  44. * Put a reference on the supplied *file*. Only referenced file pointers may be
  45. * passed to this BPF kfunc. Attempting to pass an unreferenced file pointer, or
  46. * any other arbitrary pointer for that matter, will result in the BPF program
  47. * being rejected by the BPF verifier.
  48. *
  49. * This BPF kfunc may only be called from BPF LSM programs.
  50. */
  51. __bpf_kfunc void bpf_put_file(struct file *file)
  52. {
  53. fput(file);
  54. }
  55. /**
  56. * bpf_path_d_path - resolve the pathname for the supplied path
  57. * @path: path to resolve the pathname for
  58. * @buf: buffer to return the resolved pathname in
  59. * @buf__sz: length of the supplied buffer
  60. *
  61. * Resolve the pathname for the supplied *path* and store it in *buf*. This BPF
  62. * kfunc is the safer variant of the legacy bpf_d_path() helper and should be
  63. * used in place of bpf_d_path() whenever possible. It enforces KF_TRUSTED_ARGS
  64. * semantics, meaning that the supplied *path* must itself hold a valid
  65. * reference, or else the BPF program will be outright rejected by the BPF
  66. * verifier.
  67. *
  68. * This BPF kfunc may only be called from BPF LSM programs.
  69. *
  70. * Return: A positive integer corresponding to the length of the resolved
  71. * pathname in *buf*, including the NUL termination character. On error, a
  72. * negative integer is returned.
  73. */
  74. __bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz)
  75. {
  76. int len;
  77. char *ret;
  78. if (!buf__sz)
  79. return -EINVAL;
  80. ret = d_path(path, buf, buf__sz);
  81. if (IS_ERR(ret))
  82. return PTR_ERR(ret);
  83. len = buf + buf__sz - ret;
  84. memmove(buf, ret, len);
  85. return len;
  86. }
  87. /**
  88. * bpf_get_dentry_xattr - get xattr of a dentry
  89. * @dentry: dentry to get xattr from
  90. * @name__str: name of the xattr
  91. * @value_p: output buffer of the xattr value
  92. *
  93. * Get xattr *name__str* of *dentry* and store the output in *value_ptr*.
  94. *
  95. * For security reasons, only *name__str* with prefix "user." is allowed.
  96. *
  97. * Return: 0 on success, a negative value on error.
  98. */
  99. __bpf_kfunc int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__str,
  100. struct bpf_dynptr *value_p)
  101. {
  102. struct bpf_dynptr_kern *value_ptr = (struct bpf_dynptr_kern *)value_p;
  103. struct inode *inode = d_inode(dentry);
  104. u32 value_len;
  105. void *value;
  106. int ret;
  107. if (WARN_ON(!inode))
  108. return -EINVAL;
  109. if (strncmp(name__str, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
  110. return -EPERM;
  111. value_len = __bpf_dynptr_size(value_ptr);
  112. value = __bpf_dynptr_data_rw(value_ptr, value_len);
  113. if (!value)
  114. return -EINVAL;
  115. ret = inode_permission(&nop_mnt_idmap, inode, MAY_READ);
  116. if (ret)
  117. return ret;
  118. return __vfs_getxattr(dentry, inode, name__str, value, value_len);
  119. }
  120. /**
  121. * bpf_get_file_xattr - get xattr of a file
  122. * @file: file to get xattr from
  123. * @name__str: name of the xattr
  124. * @value_p: output buffer of the xattr value
  125. *
  126. * Get xattr *name__str* of *file* and store the output in *value_ptr*.
  127. *
  128. * For security reasons, only *name__str* with prefix "user." is allowed.
  129. *
  130. * Return: 0 on success, a negative value on error.
  131. */
  132. __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str,
  133. struct bpf_dynptr *value_p)
  134. {
  135. struct dentry *dentry;
  136. dentry = file_dentry(file);
  137. return bpf_get_dentry_xattr(dentry, name__str, value_p);
  138. }
  139. __bpf_kfunc_end_defs();
  140. BTF_KFUNCS_START(bpf_fs_kfunc_set_ids)
  141. BTF_ID_FLAGS(func, bpf_get_task_exe_file,
  142. KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL)
  143. BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE)
  144. BTF_ID_FLAGS(func, bpf_path_d_path, KF_TRUSTED_ARGS)
  145. BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)
  146. BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS)
  147. BTF_KFUNCS_END(bpf_fs_kfunc_set_ids)
  148. static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
  149. {
  150. if (!btf_id_set8_contains(&bpf_fs_kfunc_set_ids, kfunc_id) ||
  151. prog->type == BPF_PROG_TYPE_LSM)
  152. return 0;
  153. return -EACCES;
  154. }
  155. static const struct btf_kfunc_id_set bpf_fs_kfunc_set = {
  156. .owner = THIS_MODULE,
  157. .set = &bpf_fs_kfunc_set_ids,
  158. .filter = bpf_fs_kfuncs_filter,
  159. };
  160. static int __init bpf_fs_kfuncs_init(void)
  161. {
  162. return register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_fs_kfunc_set);
  163. }
  164. late_initcall(bpf_fs_kfuncs_init);