security.c 2.8 KB

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