2 * security/tomoyo/securityfs_if.c
4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
7 #include <linux/security.h>
11 * tomoyo_check_task_acl - Check permission for task operation.
13 * @r: Pointer to "struct tomoyo_request_info".
14 * @ptr: Pointer to "struct tomoyo_acl_info".
16 * Returns true if granted, false otherwise.
18 static bool tomoyo_check_task_acl(struct tomoyo_request_info
*r
,
19 const struct tomoyo_acl_info
*ptr
)
21 const struct tomoyo_task_acl
*acl
= container_of(ptr
, typeof(*acl
),
23 return !tomoyo_pathcmp(r
->param
.task
.domainname
, acl
->domainname
);
27 * tomoyo_write_self - write() for /sys/kernel/security/tomoyo/self_domain interface.
29 * @file: Pointer to "struct file".
30 * @buf: Domainname to transit to.
31 * @count: Size of @buf.
34 * Returns @count on success, negative value otherwise.
36 * If domain transition was permitted but the domain transition failed, this
37 * function returns error rather than terminating current thread with SIGKILL.
39 static ssize_t
tomoyo_write_self(struct file
*file
, const char __user
*buf
,
40 size_t count
, loff_t
*ppos
)
44 if (!count
|| count
>= TOMOYO_EXEC_TMPSIZE
- 10)
46 data
= kzalloc(count
+ 1, GFP_NOFS
);
49 if (copy_from_user(data
, buf
, count
)) {
53 tomoyo_normalize_line(data
);
54 if (tomoyo_correct_domain(data
)) {
55 const int idx
= tomoyo_read_lock();
56 struct tomoyo_path_info name
;
57 struct tomoyo_request_info r
;
59 tomoyo_fill_path_info(&name
);
60 /* Check "task manual_domain_transition" permission. */
61 tomoyo_init_request_info(&r
, NULL
, TOMOYO_MAC_FILE_EXECUTE
);
62 r
.param_type
= TOMOYO_TYPE_MANUAL_TASK_ACL
;
63 r
.param
.task
.domainname
= &name
;
64 tomoyo_check_acl(&r
, tomoyo_check_task_acl
);
68 struct tomoyo_domain_info
*new_domain
=
69 tomoyo_assign_domain(data
, true);
73 struct cred
*cred
= prepare_creds();
77 struct tomoyo_domain_info
*old_domain
=
79 cred
->security
= new_domain
;
80 atomic_inc(&new_domain
->users
);
81 atomic_dec(&old_domain
->users
);
87 tomoyo_read_unlock(idx
);
92 return error
? error
: count
;
96 * tomoyo_read_self - read() for /sys/kernel/security/tomoyo/self_domain interface.
98 * @file: Pointer to "struct file".
99 * @buf: Domainname which current thread belongs to.
100 * @count: Size of @buf.
101 * @ppos: Bytes read by now.
103 * Returns read size on success, negative value otherwise.
105 static ssize_t
tomoyo_read_self(struct file
*file
, char __user
*buf
,
106 size_t count
, loff_t
*ppos
)
108 const char *domain
= tomoyo_domain()->domainname
->name
;
109 loff_t len
= strlen(domain
);
111 if (pos
>= len
|| !count
)
116 if (copy_to_user(buf
, domain
+ pos
, len
))
122 /* Operations for /sys/kernel/security/tomoyo/self_domain interface. */
123 static const struct file_operations tomoyo_self_operations
= {
124 .write
= tomoyo_write_self
,
125 .read
= tomoyo_read_self
,
129 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
131 * @inode: Pointer to "struct inode".
132 * @file: Pointer to "struct file".
134 * Returns 0 on success, negative value otherwise.
136 static int tomoyo_open(struct inode
*inode
, struct file
*file
)
138 const int key
= ((u8
*) file_inode(file
)->i_private
)
140 return tomoyo_open_control(key
, file
);
144 * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
146 * @file: Pointer to "struct file".
149 static int tomoyo_release(struct inode
*inode
, struct file
*file
)
151 tomoyo_close_control(file
->private_data
);
156 * tomoyo_poll - poll() for /sys/kernel/security/tomoyo/ interface.
158 * @file: Pointer to "struct file".
159 * @wait: Pointer to "poll_table". Maybe NULL.
161 * Returns POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM if ready to read/write,
162 * POLLOUT | POLLWRNORM otherwise.
164 static unsigned int tomoyo_poll(struct file
*file
, poll_table
*wait
)
166 return tomoyo_poll_control(file
, wait
);
170 * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
172 * @file: Pointer to "struct file".
173 * @buf: Pointer to buffer.
174 * @count: Size of @buf.
177 * Returns bytes read on success, negative value otherwise.
179 static ssize_t
tomoyo_read(struct file
*file
, char __user
*buf
, size_t count
,
182 return tomoyo_read_control(file
->private_data
, buf
, count
);
186 * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
188 * @file: Pointer to "struct file".
189 * @buf: Pointer to buffer.
190 * @count: Size of @buf.
193 * Returns @count on success, negative value otherwise.
195 static ssize_t
tomoyo_write(struct file
*file
, const char __user
*buf
,
196 size_t count
, loff_t
*ppos
)
198 return tomoyo_write_control(file
->private_data
, buf
, count
);
202 * tomoyo_operations is a "struct file_operations" which is used for handling
203 * /sys/kernel/security/tomoyo/ interface.
205 * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
206 * See tomoyo_io_buffer for internals.
208 static const struct file_operations tomoyo_operations
= {
210 .release
= tomoyo_release
,
213 .write
= tomoyo_write
,
214 .llseek
= noop_llseek
,
218 * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
220 * @name: The name of the interface file.
221 * @mode: The permission of the interface file.
222 * @parent: The parent directory.
223 * @key: Type of interface.
227 static void __init
tomoyo_create_entry(const char *name
, const umode_t mode
,
228 struct dentry
*parent
, const u8 key
)
230 securityfs_create_file(name
, mode
, parent
, ((u8
*) NULL
) + key
,
235 * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
239 static int __init
tomoyo_initerface_init(void)
241 struct dentry
*tomoyo_dir
;
243 /* Don't create securityfs entries unless registered. */
244 if (current_cred()->security
!= &tomoyo_kernel_domain
)
247 tomoyo_dir
= securityfs_create_dir("tomoyo", NULL
);
248 tomoyo_create_entry("query", 0600, tomoyo_dir
,
250 tomoyo_create_entry("domain_policy", 0600, tomoyo_dir
,
251 TOMOYO_DOMAINPOLICY
);
252 tomoyo_create_entry("exception_policy", 0600, tomoyo_dir
,
253 TOMOYO_EXCEPTIONPOLICY
);
254 tomoyo_create_entry("audit", 0400, tomoyo_dir
,
256 tomoyo_create_entry(".process_status", 0600, tomoyo_dir
,
257 TOMOYO_PROCESS_STATUS
);
258 tomoyo_create_entry("stat", 0644, tomoyo_dir
,
260 tomoyo_create_entry("profile", 0600, tomoyo_dir
,
262 tomoyo_create_entry("manager", 0600, tomoyo_dir
,
264 tomoyo_create_entry("version", 0400, tomoyo_dir
,
266 securityfs_create_file("self_domain", 0666, tomoyo_dir
, NULL
,
267 &tomoyo_self_operations
);
268 tomoyo_load_builtin_policy();
272 fs_initcall(tomoyo_initerface_init
);