policy_fs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2024 Microsoft Corporation. All rights reserved.
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/namei.h>
  7. #include <linux/types.h>
  8. #include <linux/dcache.h>
  9. #include <linux/security.h>
  10. #include "ipe.h"
  11. #include "policy.h"
  12. #include "eval.h"
  13. #include "fs.h"
  14. #define MAX_VERSION_SIZE ARRAY_SIZE("65535.65535.65535")
  15. /**
  16. * ipefs_file - defines a file in securityfs.
  17. */
  18. struct ipefs_file {
  19. const char *name;
  20. umode_t access;
  21. const struct file_operations *fops;
  22. };
  23. /**
  24. * read_pkcs7() - Read handler for "ipe/policies/$name/pkcs7".
  25. * @f: Supplies a file structure representing the securityfs node.
  26. * @data: Supplies a buffer passed to the write syscall.
  27. * @len: Supplies the length of @data.
  28. * @offset: unused.
  29. *
  30. * @data will be populated with the pkcs7 blob representing the policy
  31. * on success. If the policy is unsigned (like the boot policy), this
  32. * will return -ENOENT.
  33. *
  34. * Return:
  35. * * Length of buffer written - Success
  36. * * %-ENOENT - Policy initializing/deleted or is unsigned
  37. */
  38. static ssize_t read_pkcs7(struct file *f, char __user *data,
  39. size_t len, loff_t *offset)
  40. {
  41. const struct ipe_policy *p = NULL;
  42. struct inode *root = NULL;
  43. int rc = 0;
  44. root = d_inode(f->f_path.dentry->d_parent);
  45. inode_lock_shared(root);
  46. p = (struct ipe_policy *)root->i_private;
  47. if (!p) {
  48. rc = -ENOENT;
  49. goto out;
  50. }
  51. if (!p->pkcs7) {
  52. rc = -ENOENT;
  53. goto out;
  54. }
  55. rc = simple_read_from_buffer(data, len, offset, p->pkcs7, p->pkcs7len);
  56. out:
  57. inode_unlock_shared(root);
  58. return rc;
  59. }
  60. /**
  61. * read_policy() - Read handler for "ipe/policies/$name/policy".
  62. * @f: Supplies a file structure representing the securityfs node.
  63. * @data: Supplies a buffer passed to the write syscall.
  64. * @len: Supplies the length of @data.
  65. * @offset: unused.
  66. *
  67. * @data will be populated with the plain-text version of the policy
  68. * on success.
  69. *
  70. * Return:
  71. * * Length of buffer written - Success
  72. * * %-ENOENT - Policy initializing/deleted
  73. */
  74. static ssize_t read_policy(struct file *f, char __user *data,
  75. size_t len, loff_t *offset)
  76. {
  77. const struct ipe_policy *p = NULL;
  78. struct inode *root = NULL;
  79. int rc = 0;
  80. root = d_inode(f->f_path.dentry->d_parent);
  81. inode_lock_shared(root);
  82. p = (struct ipe_policy *)root->i_private;
  83. if (!p) {
  84. rc = -ENOENT;
  85. goto out;
  86. }
  87. rc = simple_read_from_buffer(data, len, offset, p->text, p->textlen);
  88. out:
  89. inode_unlock_shared(root);
  90. return rc;
  91. }
  92. /**
  93. * read_name() - Read handler for "ipe/policies/$name/name".
  94. * @f: Supplies a file structure representing the securityfs node.
  95. * @data: Supplies a buffer passed to the write syscall.
  96. * @len: Supplies the length of @data.
  97. * @offset: unused.
  98. *
  99. * @data will be populated with the policy_name attribute on success.
  100. *
  101. * Return:
  102. * * Length of buffer written - Success
  103. * * %-ENOENT - Policy initializing/deleted
  104. */
  105. static ssize_t read_name(struct file *f, char __user *data,
  106. size_t len, loff_t *offset)
  107. {
  108. const struct ipe_policy *p = NULL;
  109. struct inode *root = NULL;
  110. int rc = 0;
  111. root = d_inode(f->f_path.dentry->d_parent);
  112. inode_lock_shared(root);
  113. p = (struct ipe_policy *)root->i_private;
  114. if (!p) {
  115. rc = -ENOENT;
  116. goto out;
  117. }
  118. rc = simple_read_from_buffer(data, len, offset, p->parsed->name,
  119. strlen(p->parsed->name));
  120. out:
  121. inode_unlock_shared(root);
  122. return rc;
  123. }
  124. /**
  125. * read_version() - Read handler for "ipe/policies/$name/version".
  126. * @f: Supplies a file structure representing the securityfs node.
  127. * @data: Supplies a buffer passed to the write syscall.
  128. * @len: Supplies the length of @data.
  129. * @offset: unused.
  130. *
  131. * @data will be populated with the version string on success.
  132. *
  133. * Return:
  134. * * Length of buffer written - Success
  135. * * %-ENOENT - Policy initializing/deleted
  136. */
  137. static ssize_t read_version(struct file *f, char __user *data,
  138. size_t len, loff_t *offset)
  139. {
  140. char buffer[MAX_VERSION_SIZE] = { 0 };
  141. const struct ipe_policy *p = NULL;
  142. struct inode *root = NULL;
  143. size_t strsize = 0;
  144. ssize_t rc = 0;
  145. root = d_inode(f->f_path.dentry->d_parent);
  146. inode_lock_shared(root);
  147. p = (struct ipe_policy *)root->i_private;
  148. if (!p) {
  149. rc = -ENOENT;
  150. goto out;
  151. }
  152. strsize = scnprintf(buffer, ARRAY_SIZE(buffer), "%hu.%hu.%hu",
  153. p->parsed->version.major, p->parsed->version.minor,
  154. p->parsed->version.rev);
  155. rc = simple_read_from_buffer(data, len, offset, buffer, strsize);
  156. out:
  157. inode_unlock_shared(root);
  158. return rc;
  159. }
  160. /**
  161. * setactive() - Write handler for "ipe/policies/$name/active".
  162. * @f: Supplies a file structure representing the securityfs node.
  163. * @data: Supplies a buffer passed to the write syscall.
  164. * @len: Supplies the length of @data.
  165. * @offset: unused.
  166. *
  167. * Return:
  168. * * Length of buffer written - Success
  169. * * %-EPERM - Insufficient permission
  170. * * %-EINVAL - Invalid input
  171. * * %-ENOENT - Policy initializing/deleted
  172. */
  173. static ssize_t setactive(struct file *f, const char __user *data,
  174. size_t len, loff_t *offset)
  175. {
  176. const struct ipe_policy *p = NULL;
  177. struct inode *root = NULL;
  178. bool value = false;
  179. int rc = 0;
  180. if (!file_ns_capable(f, &init_user_ns, CAP_MAC_ADMIN))
  181. return -EPERM;
  182. rc = kstrtobool_from_user(data, len, &value);
  183. if (rc)
  184. return rc;
  185. if (!value)
  186. return -EINVAL;
  187. root = d_inode(f->f_path.dentry->d_parent);
  188. inode_lock(root);
  189. p = (struct ipe_policy *)root->i_private;
  190. if (!p) {
  191. rc = -ENOENT;
  192. goto out;
  193. }
  194. rc = ipe_set_active_pol(p);
  195. out:
  196. inode_unlock(root);
  197. return (rc < 0) ? rc : len;
  198. }
  199. /**
  200. * getactive() - Read handler for "ipe/policies/$name/active".
  201. * @f: Supplies a file structure representing the securityfs node.
  202. * @data: Supplies a buffer passed to the write syscall.
  203. * @len: Supplies the length of @data.
  204. * @offset: unused.
  205. *
  206. * @data will be populated with the 1 or 0 depending on if the
  207. * corresponding policy is active.
  208. *
  209. * Return:
  210. * * Length of buffer written - Success
  211. * * %-ENOENT - Policy initializing/deleted
  212. */
  213. static ssize_t getactive(struct file *f, char __user *data,
  214. size_t len, loff_t *offset)
  215. {
  216. const struct ipe_policy *p = NULL;
  217. struct inode *root = NULL;
  218. const char *str;
  219. int rc = 0;
  220. root = d_inode(f->f_path.dentry->d_parent);
  221. inode_lock_shared(root);
  222. p = (struct ipe_policy *)root->i_private;
  223. if (!p) {
  224. inode_unlock_shared(root);
  225. return -ENOENT;
  226. }
  227. inode_unlock_shared(root);
  228. str = (p == rcu_access_pointer(ipe_active_policy)) ? "1" : "0";
  229. rc = simple_read_from_buffer(data, len, offset, str, 1);
  230. return rc;
  231. }
  232. /**
  233. * update_policy() - Write handler for "ipe/policies/$name/update".
  234. * @f: Supplies a file structure representing the securityfs node.
  235. * @data: Supplies a buffer passed to the write syscall.
  236. * @len: Supplies the length of @data.
  237. * @offset: unused.
  238. *
  239. * On success this updates the policy represented by $name,
  240. * in-place.
  241. *
  242. * Return: Length of buffer written on success. If an error occurs,
  243. * the function will return the -errno.
  244. */
  245. static ssize_t update_policy(struct file *f, const char __user *data,
  246. size_t len, loff_t *offset)
  247. {
  248. struct inode *root = NULL;
  249. char *copy = NULL;
  250. int rc = 0;
  251. if (!file_ns_capable(f, &init_user_ns, CAP_MAC_ADMIN))
  252. return -EPERM;
  253. copy = memdup_user(data, len);
  254. if (IS_ERR(copy))
  255. return PTR_ERR(copy);
  256. root = d_inode(f->f_path.dentry->d_parent);
  257. inode_lock(root);
  258. rc = ipe_update_policy(root, NULL, 0, copy, len);
  259. inode_unlock(root);
  260. kfree(copy);
  261. if (rc)
  262. return rc;
  263. return len;
  264. }
  265. /**
  266. * delete_policy() - write handler for "ipe/policies/$name/delete".
  267. * @f: Supplies a file structure representing the securityfs node.
  268. * @data: Supplies a buffer passed to the write syscall.
  269. * @len: Supplies the length of @data.
  270. * @offset: unused.
  271. *
  272. * On success this deletes the policy represented by $name.
  273. *
  274. * Return:
  275. * * Length of buffer written - Success
  276. * * %-EPERM - Insufficient permission/deleting active policy
  277. * * %-EINVAL - Invalid input
  278. * * %-ENOENT - Policy initializing/deleted
  279. */
  280. static ssize_t delete_policy(struct file *f, const char __user *data,
  281. size_t len, loff_t *offset)
  282. {
  283. struct ipe_policy *ap = NULL;
  284. struct ipe_policy *p = NULL;
  285. struct inode *root = NULL;
  286. bool value = false;
  287. int rc = 0;
  288. if (!file_ns_capable(f, &init_user_ns, CAP_MAC_ADMIN))
  289. return -EPERM;
  290. rc = kstrtobool_from_user(data, len, &value);
  291. if (rc)
  292. return rc;
  293. if (!value)
  294. return -EINVAL;
  295. root = d_inode(f->f_path.dentry->d_parent);
  296. inode_lock(root);
  297. p = (struct ipe_policy *)root->i_private;
  298. if (!p) {
  299. inode_unlock(root);
  300. return -ENOENT;
  301. }
  302. mutex_lock(&ipe_policy_lock);
  303. ap = rcu_dereference_protected(ipe_active_policy,
  304. lockdep_is_held(&ipe_policy_lock));
  305. if (p == ap) {
  306. mutex_unlock(&ipe_policy_lock);
  307. inode_unlock(root);
  308. return -EPERM;
  309. }
  310. mutex_unlock(&ipe_policy_lock);
  311. root->i_private = NULL;
  312. inode_unlock(root);
  313. synchronize_rcu();
  314. ipe_free_policy(p);
  315. return len;
  316. }
  317. static const struct file_operations content_fops = {
  318. .read = read_policy,
  319. };
  320. static const struct file_operations pkcs7_fops = {
  321. .read = read_pkcs7,
  322. };
  323. static const struct file_operations name_fops = {
  324. .read = read_name,
  325. };
  326. static const struct file_operations ver_fops = {
  327. .read = read_version,
  328. };
  329. static const struct file_operations active_fops = {
  330. .write = setactive,
  331. .read = getactive,
  332. };
  333. static const struct file_operations update_fops = {
  334. .write = update_policy,
  335. };
  336. static const struct file_operations delete_fops = {
  337. .write = delete_policy,
  338. };
  339. /**
  340. * policy_subdir - files under a policy subdirectory
  341. */
  342. static const struct ipefs_file policy_subdir[] = {
  343. { "pkcs7", 0444, &pkcs7_fops },
  344. { "policy", 0444, &content_fops },
  345. { "name", 0444, &name_fops },
  346. { "version", 0444, &ver_fops },
  347. { "active", 0600, &active_fops },
  348. { "update", 0200, &update_fops },
  349. { "delete", 0200, &delete_fops },
  350. };
  351. /**
  352. * ipe_del_policyfs_node() - Delete a securityfs entry for @p.
  353. * @p: Supplies a pointer to the policy to delete a securityfs entry for.
  354. */
  355. void ipe_del_policyfs_node(struct ipe_policy *p)
  356. {
  357. securityfs_recursive_remove(p->policyfs);
  358. p->policyfs = NULL;
  359. }
  360. /**
  361. * ipe_new_policyfs_node() - Create a securityfs entry for @p.
  362. * @p: Supplies a pointer to the policy to create a securityfs entry for.
  363. *
  364. * Return: %0 on success. If an error occurs, the function will return
  365. * the -errno.
  366. */
  367. int ipe_new_policyfs_node(struct ipe_policy *p)
  368. {
  369. const struct ipefs_file *f = NULL;
  370. struct dentry *policyfs = NULL;
  371. struct inode *root = NULL;
  372. struct dentry *d = NULL;
  373. size_t i = 0;
  374. int rc = 0;
  375. if (p->policyfs)
  376. return 0;
  377. policyfs = securityfs_create_dir(p->parsed->name, policy_root);
  378. if (IS_ERR(policyfs))
  379. return PTR_ERR(policyfs);
  380. root = d_inode(policyfs);
  381. for (i = 0; i < ARRAY_SIZE(policy_subdir); ++i) {
  382. f = &policy_subdir[i];
  383. d = securityfs_create_file(f->name, f->access, policyfs,
  384. NULL, f->fops);
  385. if (IS_ERR(d)) {
  386. rc = PTR_ERR(d);
  387. goto err;
  388. }
  389. }
  390. inode_lock(root);
  391. p->policyfs = policyfs;
  392. root->i_private = p;
  393. inode_unlock(root);
  394. return 0;
  395. err:
  396. securityfs_recursive_remove(policyfs);
  397. return rc;
  398. }