security.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* CacheFiles security management
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/cred.h>
  9. #include "internal.h"
  10. /*
  11. * determine the security context within which we access the cache from within
  12. * the kernel
  13. */
  14. int cachefiles_get_security_ID(struct cachefiles_cache *cache)
  15. {
  16. struct cred *new;
  17. int ret;
  18. _enter("{%s}", cache->secctx);
  19. new = prepare_kernel_cred(current);
  20. if (!new) {
  21. ret = -ENOMEM;
  22. goto error;
  23. }
  24. if (cache->secctx) {
  25. ret = set_security_override_from_ctx(new, cache->secctx);
  26. if (ret < 0) {
  27. put_cred(new);
  28. pr_err("Security denies permission to nominate security context: error %d\n",
  29. ret);
  30. goto error;
  31. }
  32. }
  33. cache->cache_cred = new;
  34. ret = 0;
  35. error:
  36. _leave(" = %d", ret);
  37. return ret;
  38. }
  39. /*
  40. * see if mkdir and create can be performed in the root directory
  41. */
  42. static int cachefiles_check_cache_dir(struct cachefiles_cache *cache,
  43. struct dentry *root)
  44. {
  45. int ret;
  46. ret = security_inode_mkdir(d_backing_inode(root), root, 0);
  47. if (ret < 0) {
  48. pr_err("Security denies permission to make dirs: error %d",
  49. ret);
  50. return ret;
  51. }
  52. ret = security_inode_create(d_backing_inode(root), root, 0);
  53. if (ret < 0)
  54. pr_err("Security denies permission to create files: error %d",
  55. ret);
  56. return ret;
  57. }
  58. /*
  59. * check the security details of the on-disk cache
  60. * - must be called with security override in force
  61. * - must return with a security override in force - even in the case of an
  62. * error
  63. */
  64. int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
  65. struct dentry *root,
  66. const struct cred **_saved_cred)
  67. {
  68. struct cred *new;
  69. int ret;
  70. _enter("");
  71. /* duplicate the cache creds for COW (the override is currently in
  72. * force, so we can use prepare_creds() to do this) */
  73. new = prepare_creds();
  74. if (!new)
  75. return -ENOMEM;
  76. cachefiles_end_secure(cache, *_saved_cred);
  77. /* use the cache root dir's security context as the basis with
  78. * which create files */
  79. ret = set_create_files_as(new, d_backing_inode(root));
  80. if (ret < 0) {
  81. abort_creds(new);
  82. cachefiles_begin_secure(cache, _saved_cred);
  83. _leave(" = %d [cfa]", ret);
  84. return ret;
  85. }
  86. put_cred(cache->cache_cred);
  87. cache->cache_cred = new;
  88. cachefiles_begin_secure(cache, _saved_cred);
  89. ret = cachefiles_check_cache_dir(cache, root);
  90. if (ret == -EOPNOTSUPP)
  91. ret = 0;
  92. _leave(" = %d", ret);
  93. return ret;
  94. }