1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020-2024 Microsoft Corporation. All rights reserved.
6 #include <linux/dcache.h>
7 #include <linux/security.h>
15 static struct dentry
*np __ro_after_init
;
16 static struct dentry
*root __ro_after_init
;
17 struct dentry
*policy_root __ro_after_init
;
18 static struct dentry
*audit_node __ro_after_init
;
19 static struct dentry
*enforce_node __ro_after_init
;
22 * setaudit() - Write handler for the securityfs node, "ipe/success_audit"
23 * @f: Supplies a file structure representing the securityfs node.
24 * @data: Supplies a buffer passed to the write syscall.
25 * @len: Supplies the length of @data.
29 * * Length of buffer written - Success
30 * * %-EPERM - Insufficient permission
32 static ssize_t
setaudit(struct file
*f
, const char __user
*data
,
33 size_t len
, loff_t
*offset
)
38 if (!file_ns_capable(f
, &init_user_ns
, CAP_MAC_ADMIN
))
41 rc
= kstrtobool_from_user(data
, len
, &value
);
45 WRITE_ONCE(success_audit
, value
);
51 * getaudit() - Read handler for the securityfs node, "ipe/success_audit"
52 * @f: Supplies a file structure representing the securityfs node.
53 * @data: Supplies a buffer passed to the read syscall.
54 * @len: Supplies the length of @data.
57 * Return: Length of buffer written
59 static ssize_t
getaudit(struct file
*f
, char __user
*data
,
60 size_t len
, loff_t
*offset
)
64 result
= ((READ_ONCE(success_audit
)) ? "1" : "0");
66 return simple_read_from_buffer(data
, len
, offset
, result
, 1);
70 * setenforce() - Write handler for the securityfs node, "ipe/enforce"
71 * @f: Supplies a file structure representing the securityfs node.
72 * @data: Supplies a buffer passed to the write syscall.
73 * @len: Supplies the length of @data.
77 * * Length of buffer written - Success
78 * * %-EPERM - Insufficient permission
80 static ssize_t
setenforce(struct file
*f
, const char __user
*data
,
81 size_t len
, loff_t
*offset
)
84 bool new_value
, old_value
;
86 if (!file_ns_capable(f
, &init_user_ns
, CAP_MAC_ADMIN
))
89 old_value
= READ_ONCE(enforce
);
90 rc
= kstrtobool_from_user(data
, len
, &new_value
);
94 if (new_value
!= old_value
) {
95 ipe_audit_enforce(new_value
, old_value
);
96 WRITE_ONCE(enforce
, new_value
);
103 * getenforce() - Read handler for the securityfs node, "ipe/enforce"
104 * @f: Supplies a file structure representing the securityfs node.
105 * @data: Supplies a buffer passed to the read syscall.
106 * @len: Supplies the length of @data.
109 * Return: Length of buffer written
111 static ssize_t
getenforce(struct file
*f
, char __user
*data
,
112 size_t len
, loff_t
*offset
)
116 result
= ((READ_ONCE(enforce
)) ? "1" : "0");
118 return simple_read_from_buffer(data
, len
, offset
, result
, 1);
122 * new_policy() - Write handler for the securityfs node, "ipe/new_policy".
123 * @f: Supplies a file structure representing the securityfs node.
124 * @data: Supplies a buffer passed to the write syscall.
125 * @len: Supplies the length of @data.
129 * * Length of buffer written - Success
130 * * %-EPERM - Insufficient permission
131 * * %-ENOMEM - Out of memory (OOM)
132 * * %-EBADMSG - Policy is invalid
133 * * %-ERANGE - Policy version number overflow
134 * * %-EINVAL - Policy version parsing error
135 * * %-EEXIST - Same name policy already deployed
137 static ssize_t
new_policy(struct file
*f
, const char __user
*data
,
138 size_t len
, loff_t
*offset
)
140 struct ipe_policy
*p
= NULL
;
144 if (!file_ns_capable(f
, &init_user_ns
, CAP_MAC_ADMIN
))
147 copy
= memdup_user_nul(data
, len
);
149 return PTR_ERR(copy
);
151 p
= ipe_new_policy(NULL
, 0, copy
, len
);
157 rc
= ipe_new_policyfs_node(p
);
161 ipe_audit_policy_load(p
);
167 return (rc
< 0) ? rc
: len
;
170 static const struct file_operations np_fops
= {
174 static const struct file_operations audit_fops
= {
179 static const struct file_operations enforce_fops
= {
185 * ipe_init_securityfs() - Initialize IPE's securityfs tree at fsinit.
187 * Return: %0 on success. If an error occurs, the function will return
190 static int __init
ipe_init_securityfs(void)
193 struct ipe_policy
*ap
;
198 root
= securityfs_create_dir("ipe", NULL
);
204 audit_node
= securityfs_create_file("success_audit", 0600, root
,
206 if (IS_ERR(audit_node
)) {
207 rc
= PTR_ERR(audit_node
);
211 enforce_node
= securityfs_create_file("enforce", 0600, root
, NULL
,
213 if (IS_ERR(enforce_node
)) {
214 rc
= PTR_ERR(enforce_node
);
218 policy_root
= securityfs_create_dir("policies", root
);
219 if (IS_ERR(policy_root
)) {
220 rc
= PTR_ERR(policy_root
);
224 ap
= rcu_access_pointer(ipe_active_policy
);
226 rc
= ipe_new_policyfs_node(ap
);
231 np
= securityfs_create_file("new_policy", 0200, root
, NULL
, &np_fops
);
239 securityfs_remove(np
);
240 securityfs_remove(policy_root
);
241 securityfs_remove(enforce_node
);
242 securityfs_remove(audit_node
);
243 securityfs_remove(root
);
247 fs_initcall(ipe_init_securityfs
);