77#include <linux/security.h>
88#include "common.h"
99
10+ /**
11+ * tomoyo_check_task_acl - Check permission for task operation.
12+ *
13+ * @r: Pointer to "struct tomoyo_request_info".
14+ * @ptr: Pointer to "struct tomoyo_acl_info".
15+ *
16+ * Returns true if granted, false otherwise.
17+ */
18+ static bool tomoyo_check_task_acl (struct tomoyo_request_info * r ,
19+ const struct tomoyo_acl_info * ptr )
20+ {
21+ const struct tomoyo_task_acl * acl = container_of (ptr , typeof (* acl ),
22+ head );
23+ return !tomoyo_pathcmp (r -> param .task .domainname , acl -> domainname );
24+ }
25+
26+ /**
27+ * tomoyo_write_self - write() for /sys/kernel/security/tomoyo/self_domain interface.
28+ *
29+ * @file: Pointer to "struct file".
30+ * @buf: Domainname to transit to.
31+ * @count: Size of @buf.
32+ * @ppos: Unused.
33+ *
34+ * Returns @count on success, negative value otherwise.
35+ *
36+ * If domain transition was permitted but the domain transition failed, this
37+ * function returns error rather than terminating current thread with SIGKILL.
38+ */
39+ static ssize_t tomoyo_write_self (struct file * file , const char __user * buf ,
40+ size_t count , loff_t * ppos )
41+ {
42+ char * data ;
43+ int error ;
44+ if (!count || count >= TOMOYO_EXEC_TMPSIZE - 10 )
45+ return - ENOMEM ;
46+ data = kzalloc (count + 1 , GFP_NOFS );
47+ if (!data )
48+ return - ENOMEM ;
49+ if (copy_from_user (data , buf , count )) {
50+ error = - EFAULT ;
51+ goto out ;
52+ }
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 ;
58+ name .name = data ;
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 );
65+ if (!r .granted )
66+ error = - EPERM ;
67+ else {
68+ struct tomoyo_domain_info * new_domain =
69+ tomoyo_assign_domain (data , true);
70+ if (!new_domain ) {
71+ error = - ENOENT ;
72+ } else {
73+ struct cred * cred = prepare_creds ();
74+ if (!cred ) {
75+ error = - ENOMEM ;
76+ } else {
77+ struct tomoyo_domain_info * old_domain =
78+ cred -> security ;
79+ cred -> security = new_domain ;
80+ atomic_inc (& new_domain -> users );
81+ atomic_dec (& old_domain -> users );
82+ commit_creds (cred );
83+ error = 0 ;
84+ }
85+ }
86+ }
87+ tomoyo_read_unlock (idx );
88+ } else
89+ error = - EINVAL ;
90+ out :
91+ kfree (data );
92+ return error ? error : count ;
93+ }
94+
95+ /**
96+ * tomoyo_read_self - read() for /sys/kernel/security/tomoyo/self_domain interface.
97+ *
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.
102+ *
103+ * Returns read size on success, negative value otherwise.
104+ */
105+ static ssize_t tomoyo_read_self (struct file * file , char __user * buf ,
106+ size_t count , loff_t * ppos )
107+ {
108+ const char * domain = tomoyo_domain ()-> domainname -> name ;
109+ loff_t len = strlen (domain );
110+ loff_t pos = * ppos ;
111+ if (pos >= len || !count )
112+ return 0 ;
113+ len -= pos ;
114+ if (count < len )
115+ len = count ;
116+ if (copy_to_user (buf , domain + pos , len ))
117+ return - EFAULT ;
118+ * ppos += len ;
119+ return len ;
120+ }
121+
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 ,
126+ };
127+
10128/**
11129 * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
12130 *
@@ -135,8 +253,6 @@ static int __init tomoyo_initerface_init(void)
135253 TOMOYO_EXCEPTIONPOLICY );
136254 tomoyo_create_entry ("audit" , 0400 , tomoyo_dir ,
137255 TOMOYO_AUDIT );
138- tomoyo_create_entry ("self_domain" , 0400 , tomoyo_dir ,
139- TOMOYO_SELFDOMAIN );
140256 tomoyo_create_entry (".process_status" , 0600 , tomoyo_dir ,
141257 TOMOYO_PROCESS_STATUS );
142258 tomoyo_create_entry ("stat" , 0644 , tomoyo_dir ,
@@ -147,6 +263,8 @@ static int __init tomoyo_initerface_init(void)
147263 TOMOYO_MANAGER );
148264 tomoyo_create_entry ("version" , 0400 , tomoyo_dir ,
149265 TOMOYO_VERSION );
266+ securityfs_create_file ("self_domain" , 0666 , tomoyo_dir , NULL ,
267+ & tomoyo_self_operations );
150268 return 0 ;
151269}
152270
0 commit comments