1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* CacheFiles security management
4 * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
9 #include <linux/cred.h>
13 * determine the security context within which we access the cache from within
16 int cachefiles_get_security_ID(struct cachefiles_cache *cache)
21 _enter("{%s}", cache->secctx);
23 new = prepare_kernel_cred(current);
30 ret = set_security_override_from_ctx(new, cache->secctx);
33 pr_err("Security denies permission to nominate security context: error %d\n",
39 cache->cache_cred = new;
47 * see if mkdir and create can be performed in the root directory
49 static int cachefiles_check_cache_dir(struct cachefiles_cache *cache,
54 ret = security_inode_mkdir(d_backing_inode(root), root, 0);
56 pr_err("Security denies permission to make dirs: error %d",
61 ret = security_inode_create(d_backing_inode(root), root, 0);
63 pr_err("Security denies permission to create files: error %d",
70 * check the security details of the on-disk cache
71 * - must be called with security override in force
72 * - must return with a security override in force - even in the case of an
75 int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
77 const struct cred **_saved_cred)
84 /* duplicate the cache creds for COW (the override is currently in
85 * force, so we can use prepare_creds() to do this) */
86 new = prepare_creds();
90 cachefiles_end_secure(cache, *_saved_cred);
92 /* use the cache root dir's security context as the basis with
93 * which create files */
94 ret = set_create_files_as(new, d_backing_inode(root));
97 cachefiles_begin_secure(cache, _saved_cred);
98 _leave(" = %d [cfa]", ret);
102 put_cred(cache->cache_cred);
103 cache->cache_cred = new;
105 cachefiles_begin_secure(cache, _saved_cred);
106 ret = cachefiles_check_cache_dir(cache, root);
108 if (ret == -EOPNOTSUPP)
110 _leave(" = %d", ret);