fixup over ("LiteX: driver for LiteVideo")
[linux/fpc-iii.git] / fs / cachefiles / security.c
blobaec13fd9469241deb8e3b7e54fd621e87402318b
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* CacheFiles security management
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
8 #include <linux/fs.h>
9 #include <linux/cred.h>
10 #include "internal.h"
13 * determine the security context within which we access the cache from within
14 * the kernel
16 int cachefiles_get_security_ID(struct cachefiles_cache *cache)
18 struct cred *new;
19 int ret;
21 _enter("{%s}", cache->secctx);
23 new = prepare_kernel_cred(current);
24 if (!new) {
25 ret = -ENOMEM;
26 goto error;
29 if (cache->secctx) {
30 ret = set_security_override_from_ctx(new, cache->secctx);
31 if (ret < 0) {
32 put_cred(new);
33 pr_err("Security denies permission to nominate security context: error %d\n",
34 ret);
35 goto error;
39 cache->cache_cred = new;
40 ret = 0;
41 error:
42 _leave(" = %d", ret);
43 return ret;
47 * see if mkdir and create can be performed in the root directory
49 static int cachefiles_check_cache_dir(struct cachefiles_cache *cache,
50 struct dentry *root)
52 int ret;
54 ret = security_inode_mkdir(d_backing_inode(root), root, 0);
55 if (ret < 0) {
56 pr_err("Security denies permission to make dirs: error %d",
57 ret);
58 return ret;
61 ret = security_inode_create(d_backing_inode(root), root, 0);
62 if (ret < 0)
63 pr_err("Security denies permission to create files: error %d",
64 ret);
66 return ret;
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
73 * error
75 int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
76 struct dentry *root,
77 const struct cred **_saved_cred)
79 struct cred *new;
80 int ret;
82 _enter("");
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();
87 if (!new)
88 return -ENOMEM;
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));
95 if (ret < 0) {
96 abort_creds(new);
97 cachefiles_begin_secure(cache, _saved_cred);
98 _leave(" = %d [cfa]", ret);
99 return 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)
109 ret = 0;
110 _leave(" = %d", ret);
111 return ret;