9967c1c5c713bac56e25bf90c5f8b16d9f513d43
[firefly-linux-kernel-4.4.55.git] / security / tomoyo / securityfs_if.c
1 /*
2  * security/tomoyo/common.c
3  *
4  * Securityfs interface for TOMOYO.
5  *
6  * Copyright (C) 2005-2010  NTT DATA CORPORATION
7  */
8
9 #include <linux/security.h>
10 #include "common.h"
11
12 /**
13  * tomoyo_open - open() for /sys/kernel/security/tomoyo/ interface.
14  *
15  * @inode: Pointer to "struct inode".
16  * @file:  Pointer to "struct file".
17  *
18  * Returns 0 on success, negative value otherwise.
19  */
20 static int tomoyo_open(struct inode *inode, struct file *file)
21 {
22         const int key = ((u8 *) file->f_path.dentry->d_inode->i_private)
23                 - ((u8 *) NULL);
24         return tomoyo_open_control(key, file);
25 }
26
27 /**
28  * tomoyo_release - close() for /sys/kernel/security/tomoyo/ interface.
29  *
30  * @inode: Pointer to "struct inode".
31  * @file:  Pointer to "struct file".
32  *
33  * Returns 0 on success, negative value otherwise.
34  */
35 static int tomoyo_release(struct inode *inode, struct file *file)
36 {
37         return tomoyo_close_control(file);
38 }
39
40 /**
41  * tomoyo_poll - poll() for /proc/ccs/ interface.
42  *
43  * @file: Pointer to "struct file".
44  * @wait: Pointer to "poll_table".
45  *
46  * Returns 0 on success, negative value otherwise.
47  */
48 static unsigned int tomoyo_poll(struct file *file, poll_table *wait)
49 {
50         return tomoyo_poll_control(file, wait);
51 }
52
53 /**
54  * tomoyo_read - read() for /sys/kernel/security/tomoyo/ interface.
55  *
56  * @file:  Pointer to "struct file".
57  * @buf:   Pointer to buffer.
58  * @count: Size of @buf.
59  * @ppos:  Unused.
60  *
61  * Returns bytes read on success, negative value otherwise.
62  */
63 static ssize_t tomoyo_read(struct file *file, char __user *buf, size_t count,
64                            loff_t *ppos)
65 {
66         return tomoyo_read_control(file, buf, count);
67 }
68
69 /**
70  * tomoyo_write - write() for /sys/kernel/security/tomoyo/ interface.
71  *
72  * @file:  Pointer to "struct file".
73  * @buf:   Pointer to buffer.
74  * @count: Size of @buf.
75  * @ppos:  Unused.
76  *
77  * Returns @count on success, negative value otherwise.
78  */
79 static ssize_t tomoyo_write(struct file *file, const char __user *buf,
80                             size_t count, loff_t *ppos)
81 {
82         return tomoyo_write_control(file, buf, count);
83 }
84
85 /*
86  * tomoyo_operations is a "struct file_operations" which is used for handling
87  * /sys/kernel/security/tomoyo/ interface.
88  *
89  * Some files under /sys/kernel/security/tomoyo/ directory accept open(O_RDWR).
90  * See tomoyo_io_buffer for internals.
91  */
92 static const struct file_operations tomoyo_operations = {
93         .open    = tomoyo_open,
94         .release = tomoyo_release,
95         .poll    = tomoyo_poll,
96         .read    = tomoyo_read,
97         .write   = tomoyo_write,
98 };
99
100 /**
101  * tomoyo_create_entry - Create interface files under /sys/kernel/security/tomoyo/ directory.
102  *
103  * @name:   The name of the interface file.
104  * @mode:   The permission of the interface file.
105  * @parent: The parent directory.
106  * @key:    Type of interface.
107  *
108  * Returns nothing.
109  */
110 static void __init tomoyo_create_entry(const char *name, const mode_t mode,
111                                        struct dentry *parent, const u8 key)
112 {
113         securityfs_create_file(name, mode, parent, ((u8 *) NULL) + key,
114                                &tomoyo_operations);
115 }
116
117 /**
118  * tomoyo_initerface_init - Initialize /sys/kernel/security/tomoyo/ interface.
119  *
120  * Returns 0.
121  */
122 static int __init tomoyo_initerface_init(void)
123 {
124         struct dentry *tomoyo_dir;
125
126         /* Don't create securityfs entries unless registered. */
127         if (current_cred()->security != &tomoyo_kernel_domain)
128                 return 0;
129
130         tomoyo_dir = securityfs_create_dir("tomoyo", NULL);
131         tomoyo_create_entry("query",            0600, tomoyo_dir,
132                             TOMOYO_QUERY);
133         tomoyo_create_entry("domain_policy",    0600, tomoyo_dir,
134                             TOMOYO_DOMAINPOLICY);
135         tomoyo_create_entry("exception_policy", 0600, tomoyo_dir,
136                             TOMOYO_EXCEPTIONPOLICY);
137         tomoyo_create_entry("self_domain",      0400, tomoyo_dir,
138                             TOMOYO_SELFDOMAIN);
139         tomoyo_create_entry(".domain_status",   0600, tomoyo_dir,
140                             TOMOYO_DOMAIN_STATUS);
141         tomoyo_create_entry(".process_status",  0600, tomoyo_dir,
142                             TOMOYO_PROCESS_STATUS);
143         tomoyo_create_entry("meminfo",          0600, tomoyo_dir,
144                             TOMOYO_MEMINFO);
145         tomoyo_create_entry("profile",          0600, tomoyo_dir,
146                             TOMOYO_PROFILE);
147         tomoyo_create_entry("manager",          0600, tomoyo_dir,
148                             TOMOYO_MANAGER);
149         tomoyo_create_entry("version",          0400, tomoyo_dir,
150                             TOMOYO_VERSION);
151         return 0;
152 }
153
154 fs_initcall(tomoyo_initerface_init);