blob: 6ab9e4cdd61fffbf04d14e190ba81f33e7af2798 [file] [log] [blame]
Kentaro Takedab69a54e2009-02-05 17:18:14 +09001/*
2 * security/tomoyo/file.c
3 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09004 * Pathname restriction functions.
Kentaro Takedab69a54e2009-02-05 17:18:14 +09005 *
Tetsuo Handac3ef1502010-05-17 10:12:46 +09006 * Copyright (C) 2005-2010 NTT DATA CORPORATION
Kentaro Takedab69a54e2009-02-05 17:18:14 +09007 */
8
9#include "common.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Kentaro Takedab69a54e2009-02-05 17:18:14 +090011
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090012/*
13 * Mapping table from "enum tomoyo_path_acl_index" to "enum tomoyo_mac_index".
14 */
Tetsuo Handa57c25902010-06-03 20:38:44 +090015static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090016 [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE,
17 [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN,
18 [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN,
Tetsuo Handa7c759642011-06-26 23:15:31 +090019 [TOMOYO_TYPE_APPEND] = TOMOYO_MAC_FILE_OPEN,
Tetsuo Handa57c25902010-06-03 20:38:44 +090020 [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK,
Tetsuo Handa7c759642011-06-26 23:15:31 +090021 [TOMOYO_TYPE_GETATTR] = TOMOYO_MAC_FILE_GETATTR,
Tetsuo Handa57c25902010-06-03 20:38:44 +090022 [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR,
23 [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE,
24 [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK,
Tetsuo Handa57c25902010-06-03 20:38:44 +090025 [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT,
26 [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT,
27};
28
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090029/*
30 * Mapping table from "enum tomoyo_mkdev_acl_index" to "enum tomoyo_mac_index".
31 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090032const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090033 [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
34 [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR,
35};
36
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090037/*
38 * Mapping table from "enum tomoyo_path2_acl_index" to "enum tomoyo_mac_index".
39 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090040const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090041 [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK,
42 [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME,
43 [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
44};
45
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090046/*
47 * Mapping table from "enum tomoyo_path_number_acl_index" to
48 * "enum tomoyo_mac_index".
49 */
Tetsuo Handa0d2171d2011-06-26 23:17:46 +090050const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
Tetsuo Handa57c25902010-06-03 20:38:44 +090051 [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
52 [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR,
53 [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
54 [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
55 [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL,
56 [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD,
57 [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN,
58 [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP,
59};
60
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090061/**
62 * tomoyo_put_name_union - Drop reference on "struct tomoyo_name_union".
63 *
64 * @ptr: Pointer to "struct tomoyo_name_union".
65 *
66 * Returns nothing.
67 */
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090068void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
69{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090070 tomoyo_put_group(ptr->group);
71 tomoyo_put_name(ptr->filename);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090072}
73
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090074/**
75 * tomoyo_compare_name_union - Check whether a name matches "struct tomoyo_name_union" or not.
76 *
77 * @name: Pointer to "struct tomoyo_path_info".
78 * @ptr: Pointer to "struct tomoyo_name_union".
79 *
80 * Returns "struct tomoyo_path_info" if @name matches @ptr, NULL otherwise.
81 */
Tetsuo Handa484ca792010-07-29 14:29:55 +090082const struct tomoyo_path_info *
83tomoyo_compare_name_union(const struct tomoyo_path_info *name,
84 const struct tomoyo_name_union *ptr)
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090085{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090086 if (ptr->group)
Tetsuo Handa3f629632010-06-03 20:37:26 +090087 return tomoyo_path_matches_group(name, ptr->group);
Tetsuo Handa484ca792010-07-29 14:29:55 +090088 if (tomoyo_path_matches_pattern(name, ptr->filename))
89 return ptr->filename;
90 return NULL;
Tetsuo Handa7762fbf2010-05-10 17:30:26 +090091}
92
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +090093/**
94 * tomoyo_put_number_union - Drop reference on "struct tomoyo_number_union".
95 *
96 * @ptr: Pointer to "struct tomoyo_number_union".
97 *
98 * Returns nothing.
99 */
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900100void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
101{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900102 tomoyo_put_group(ptr->group);
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900103}
104
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900105/**
106 * tomoyo_compare_number_union - Check whether a value matches "struct tomoyo_number_union" or not.
107 *
108 * @value: Number to check.
109 * @ptr: Pointer to "struct tomoyo_number_union".
110 *
111 * Returns true if @value matches @ptr, false otherwise.
112 */
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900113bool tomoyo_compare_number_union(const unsigned long value,
114 const struct tomoyo_number_union *ptr)
115{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900116 if (ptr->group)
Tetsuo Handa4c3e9e22010-05-17 10:06:58 +0900117 return tomoyo_number_matches_group(value, value, ptr->group);
118 return value >= ptr->values[0] && value <= ptr->values[1];
119}
120
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900121/**
122 * tomoyo_add_slash - Add trailing '/' if needed.
123 *
124 * @buf: Pointer to "struct tomoyo_path_info".
125 *
126 * Returns nothing.
127 *
128 * @buf must be generated by tomoyo_encode() because this function does not
129 * allocate memory for adding '/'.
130 */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900131static void tomoyo_add_slash(struct tomoyo_path_info *buf)
132{
133 if (buf->is_dir)
134 return;
135 /*
136 * This is OK because tomoyo_encode() reserves space for appending "/".
137 */
138 strcat((char *) buf->name, "/");
139 tomoyo_fill_path_info(buf);
140}
141
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900142/**
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900143 * tomoyo_get_realpath - Get realpath.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900144 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900145 * @buf: Pointer to "struct tomoyo_path_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900146 * @path: Pointer to "struct path".
147 *
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900148 * Returns true on success, false otherwise.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900149 */
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900150static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900151{
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900152 buf->name = tomoyo_realpath_from_path(path);
153 if (buf->name) {
154 tomoyo_fill_path_info(buf);
155 return true;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900156 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900157 return false;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900158}
159
Tetsuo Handa99a85252010-06-16 16:22:51 +0900160/**
161 * tomoyo_audit_path_log - Audit path request log.
162 *
163 * @r: Pointer to "struct tomoyo_request_info".
164 *
165 * Returns 0 on success, negative value otherwise.
166 */
167static int tomoyo_audit_path_log(struct tomoyo_request_info *r)
168{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900169 return tomoyo_supervisor(r, "file %s %s\n", tomoyo_path_keyword
170 [r->param.path.operation],
171 r->param.path.filename->name);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900172}
173
174/**
175 * tomoyo_audit_path2_log - Audit path/path request log.
176 *
177 * @r: Pointer to "struct tomoyo_request_info".
178 *
179 * Returns 0 on success, negative value otherwise.
180 */
181static int tomoyo_audit_path2_log(struct tomoyo_request_info *r)
182{
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900183 return tomoyo_supervisor(r, "file %s %s %s\n", tomoyo_mac_keywords
184 [tomoyo_pp2mac[r->param.path2.operation]],
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900185 r->param.path2.filename1->name,
186 r->param.path2.filename2->name);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900187}
188
189/**
190 * tomoyo_audit_mkdev_log - Audit path/number/number/number request log.
191 *
192 * @r: Pointer to "struct tomoyo_request_info".
193 *
194 * Returns 0 on success, negative value otherwise.
195 */
196static int tomoyo_audit_mkdev_log(struct tomoyo_request_info *r)
197{
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900198 return tomoyo_supervisor(r, "file %s %s 0%o %u %u\n",
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900199 tomoyo_mac_keywords
200 [tomoyo_pnnn2mac[r->param.mkdev.operation]],
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900201 r->param.mkdev.filename->name,
202 r->param.mkdev.mode, r->param.mkdev.major,
203 r->param.mkdev.minor);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900204}
205
206/**
207 * tomoyo_audit_path_number_log - Audit path/number request log.
208 *
Tetsuo Handab5bc60b2011-06-26 23:16:03 +0900209 * @r: Pointer to "struct tomoyo_request_info".
Tetsuo Handa99a85252010-06-16 16:22:51 +0900210 *
211 * Returns 0 on success, negative value otherwise.
212 */
213static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r)
214{
215 const u8 type = r->param.path_number.operation;
216 u8 radix;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900217 char buffer[64];
Tetsuo Handa99a85252010-06-16 16:22:51 +0900218 switch (type) {
219 case TOMOYO_TYPE_CREATE:
220 case TOMOYO_TYPE_MKDIR:
221 case TOMOYO_TYPE_MKFIFO:
222 case TOMOYO_TYPE_MKSOCK:
223 case TOMOYO_TYPE_CHMOD:
224 radix = TOMOYO_VALUE_TYPE_OCTAL;
225 break;
226 case TOMOYO_TYPE_IOCTL:
227 radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
228 break;
229 default:
230 radix = TOMOYO_VALUE_TYPE_DECIMAL;
231 break;
232 }
233 tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number,
234 radix);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900235 return tomoyo_supervisor(r, "file %s %s %s\n", tomoyo_mac_keywords
236 [tomoyo_pn2mac[type]],
Tetsuo Handaeadd99c2011-06-26 23:18:58 +0900237 r->param.path_number.filename->name, buffer);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900238}
239
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900240/**
241 * tomoyo_check_path_acl - Check permission for path operation.
242 *
243 * @r: Pointer to "struct tomoyo_request_info".
244 * @ptr: Pointer to "struct tomoyo_acl_info".
245 *
246 * Returns true if granted, false otherwise.
247 *
248 * To be able to use wildcard for domain transition, this function sets
249 * matching entry on success. Since the caller holds tomoyo_read_lock(),
250 * it is safe to set matching entry.
251 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900252static bool tomoyo_check_path_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900253 const struct tomoyo_acl_info *ptr)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900254{
Tetsuo Handa99a85252010-06-16 16:22:51 +0900255 const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
256 head);
Tetsuo Handa484ca792010-07-29 14:29:55 +0900257 if (acl->perm & (1 << r->param.path.operation)) {
258 r->param.path.matched_path =
259 tomoyo_compare_name_union(r->param.path.filename,
260 &acl->name);
261 return r->param.path.matched_path != NULL;
262 }
263 return false;
Tetsuo Handa99a85252010-06-16 16:22:51 +0900264}
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900265
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900266/**
267 * tomoyo_check_path_number_acl - Check permission for path number operation.
268 *
269 * @r: Pointer to "struct tomoyo_request_info".
270 * @ptr: Pointer to "struct tomoyo_acl_info".
271 *
272 * Returns true if granted, false otherwise.
273 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900274static bool tomoyo_check_path_number_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900275 const struct tomoyo_acl_info *ptr)
276{
277 const struct tomoyo_path_number_acl *acl =
278 container_of(ptr, typeof(*acl), head);
279 return (acl->perm & (1 << r->param.path_number.operation)) &&
280 tomoyo_compare_number_union(r->param.path_number.number,
281 &acl->number) &&
282 tomoyo_compare_name_union(r->param.path_number.filename,
283 &acl->name);
284}
285
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900286/**
287 * tomoyo_check_path2_acl - Check permission for path path operation.
288 *
289 * @r: Pointer to "struct tomoyo_request_info".
290 * @ptr: Pointer to "struct tomoyo_acl_info".
291 *
292 * Returns true if granted, false otherwise.
293 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900294static bool tomoyo_check_path2_acl(struct tomoyo_request_info *r,
Tetsuo Handa99a85252010-06-16 16:22:51 +0900295 const struct tomoyo_acl_info *ptr)
296{
297 const struct tomoyo_path2_acl *acl =
298 container_of(ptr, typeof(*acl), head);
299 return (acl->perm & (1 << r->param.path2.operation)) &&
300 tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
301 && tomoyo_compare_name_union(r->param.path2.filename2,
302 &acl->name2);
303}
304
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900305/**
306 * tomoyo_check_mkdev_acl - Check permission for path number number number operation.
307 *
308 * @r: Pointer to "struct tomoyo_request_info".
309 * @ptr: Pointer to "struct tomoyo_acl_info".
310 *
311 * Returns true if granted, false otherwise.
312 */
Tetsuo Handa484ca792010-07-29 14:29:55 +0900313static bool tomoyo_check_mkdev_acl(struct tomoyo_request_info *r,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900314 const struct tomoyo_acl_info *ptr)
Tetsuo Handa99a85252010-06-16 16:22:51 +0900315{
Tetsuo Handa75093152010-06-16 16:23:55 +0900316 const struct tomoyo_mkdev_acl *acl =
Tetsuo Handa99a85252010-06-16 16:22:51 +0900317 container_of(ptr, typeof(*acl), head);
318 return (acl->perm & (1 << r->param.mkdev.operation)) &&
319 tomoyo_compare_number_union(r->param.mkdev.mode,
320 &acl->mode) &&
321 tomoyo_compare_number_union(r->param.mkdev.major,
322 &acl->major) &&
323 tomoyo_compare_number_union(r->param.mkdev.minor,
324 &acl->minor) &&
325 tomoyo_compare_name_union(r->param.mkdev.filename,
326 &acl->name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900327}
328
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900329/**
330 * tomoyo_same_path_acl - Check for duplicated "struct tomoyo_path_acl" entry.
331 *
332 * @a: Pointer to "struct tomoyo_acl_info".
333 * @b: Pointer to "struct tomoyo_acl_info".
334 *
335 * Returns true if @a == @b except permission bits, false otherwise.
336 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900337static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
338 const struct tomoyo_acl_info *b)
339{
340 const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
341 const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900342 return tomoyo_same_name_union(&p1->name, &p2->name);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900343}
344
Tetsuo Handa7c759642011-06-26 23:15:31 +0900345/**
346 * tomoyo_merge_path_acl - Merge duplicated "struct tomoyo_path_acl" entry.
347 *
348 * @a: Pointer to "struct tomoyo_acl_info".
349 * @b: Pointer to "struct tomoyo_acl_info".
350 * @is_delete: True for @a &= ~@b, false for @a |= @b.
351 *
352 * Returns true if @a is empty, false otherwise.
353 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900354static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
355 struct tomoyo_acl_info *b,
356 const bool is_delete)
357{
358 u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
359 ->perm;
360 u16 perm = *a_perm;
361 const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900362 if (is_delete)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900363 perm &= ~b_perm;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900364 else
Tetsuo Handa237ab452010-06-12 20:46:22 +0900365 perm |= b_perm;
Tetsuo Handa237ab452010-06-12 20:46:22 +0900366 *a_perm = perm;
367 return !perm;
368}
369
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900370/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900371 * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900372 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900373 * @perm: Permission.
374 * @param: Pointer to "struct tomoyo_acl_param".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900375 *
376 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900377 *
378 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900379 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900380static int tomoyo_update_path_acl(const u16 perm,
381 struct tomoyo_acl_param *param)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900382{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900383 struct tomoyo_path_acl e = {
384 .head.type = TOMOYO_TYPE_PATH_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900385 .perm = perm
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900386 };
Tetsuo Handa237ab452010-06-12 20:46:22 +0900387 int error;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900388 if (!tomoyo_parse_name_union(param, &e.name))
389 error = -EINVAL;
390 else
391 error = tomoyo_update_domain(&e.head, sizeof(e), param,
392 tomoyo_same_path_acl,
393 tomoyo_merge_path_acl);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900394 tomoyo_put_name_union(&e.name);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900395 return error;
396}
397
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900398/**
399 * tomoyo_same_mkdev_acl - Check for duplicated "struct tomoyo_mkdev_acl" entry.
400 *
401 * @a: Pointer to "struct tomoyo_acl_info".
402 * @b: Pointer to "struct tomoyo_acl_info".
403 *
404 * Returns true if @a == @b except permission bits, false otherwise.
405 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900406static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900407 const struct tomoyo_acl_info *b)
408{
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900409 const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1), head);
410 const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2), head);
411 return tomoyo_same_name_union(&p1->name, &p2->name) &&
412 tomoyo_same_number_union(&p1->mode, &p2->mode) &&
413 tomoyo_same_number_union(&p1->major, &p2->major) &&
414 tomoyo_same_number_union(&p1->minor, &p2->minor);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900415}
416
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900417/**
418 * tomoyo_merge_mkdev_acl - Merge duplicated "struct tomoyo_mkdev_acl" entry.
419 *
420 * @a: Pointer to "struct tomoyo_acl_info".
421 * @b: Pointer to "struct tomoyo_acl_info".
422 * @is_delete: True for @a &= ~@b, false for @a |= @b.
423 *
424 * Returns true if @a is empty, false otherwise.
425 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900426static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900427 struct tomoyo_acl_info *b,
428 const bool is_delete)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900429{
Tetsuo Handa75093152010-06-16 16:23:55 +0900430 u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl,
Tetsuo Handa237ab452010-06-12 20:46:22 +0900431 head)->perm;
432 u8 perm = *a_perm;
Tetsuo Handa75093152010-06-16 16:23:55 +0900433 const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
Tetsuo Handa237ab452010-06-12 20:46:22 +0900434 ->perm;
435 if (is_delete)
436 perm &= ~b_perm;
437 else
438 perm |= b_perm;
439 *a_perm = perm;
440 return !perm;
441}
442
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900443/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900444 * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900445 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900446 * @perm: Permission.
447 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900448 *
449 * Returns 0 on success, negative value otherwise.
Tetsuo Handa237ab452010-06-12 20:46:22 +0900450 *
451 * Caller holds tomoyo_read_lock().
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900452 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900453static int tomoyo_update_mkdev_acl(const u8 perm,
454 struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900455{
Tetsuo Handa75093152010-06-16 16:23:55 +0900456 struct tomoyo_mkdev_acl e = {
457 .head.type = TOMOYO_TYPE_MKDEV_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900458 .perm = perm
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900459 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900460 int error;
461 if (!tomoyo_parse_name_union(param, &e.name) ||
462 !tomoyo_parse_number_union(param, &e.mode) ||
463 !tomoyo_parse_number_union(param, &e.major) ||
464 !tomoyo_parse_number_union(param, &e.minor))
465 error = -EINVAL;
466 else
467 error = tomoyo_update_domain(&e.head, sizeof(e), param,
468 tomoyo_same_mkdev_acl,
469 tomoyo_merge_mkdev_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900470 tomoyo_put_name_union(&e.name);
471 tomoyo_put_number_union(&e.mode);
472 tomoyo_put_number_union(&e.major);
473 tomoyo_put_number_union(&e.minor);
474 return error;
475}
476
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900477/**
478 * tomoyo_same_path2_acl - Check for duplicated "struct tomoyo_path2_acl" entry.
479 *
480 * @a: Pointer to "struct tomoyo_acl_info".
481 * @b: Pointer to "struct tomoyo_acl_info".
482 *
483 * Returns true if @a == @b except permission bits, false otherwise.
484 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900485static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
486 const struct tomoyo_acl_info *b)
487{
488 const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
489 const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900490 return tomoyo_same_name_union(&p1->name1, &p2->name1) &&
491 tomoyo_same_name_union(&p1->name2, &p2->name2);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900492}
493
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900494/**
495 * tomoyo_merge_path2_acl - Merge duplicated "struct tomoyo_path2_acl" entry.
496 *
497 * @a: Pointer to "struct tomoyo_acl_info".
498 * @b: Pointer to "struct tomoyo_acl_info".
499 * @is_delete: True for @a &= ~@b, false for @a |= @b.
500 *
501 * Returns true if @a is empty, false otherwise.
502 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900503static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
504 struct tomoyo_acl_info *b,
505 const bool is_delete)
506{
507 u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
508 ->perm;
509 u8 perm = *a_perm;
510 const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
511 if (is_delete)
512 perm &= ~b_perm;
513 else
514 perm |= b_perm;
515 *a_perm = perm;
516 return !perm;
517}
518
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900519/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900520 * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900521 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900522 * @perm: Permission.
523 * @param: Pointer to "struct tomoyo_acl_param".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900524 *
525 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900526 *
527 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900528 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900529static int tomoyo_update_path2_acl(const u8 perm,
530 struct tomoyo_acl_param *param)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900531{
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900532 struct tomoyo_path2_acl e = {
533 .head.type = TOMOYO_TYPE_PATH2_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900534 .perm = perm
Tetsuo Handa9e4b50e2010-05-06 12:40:02 +0900535 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900536 int error;
537 if (!tomoyo_parse_name_union(param, &e.name1) ||
538 !tomoyo_parse_name_union(param, &e.name2))
539 error = -EINVAL;
540 else
541 error = tomoyo_update_domain(&e.head, sizeof(e), param,
542 tomoyo_same_path2_acl,
543 tomoyo_merge_path2_acl);
Tetsuo Handa7762fbf2010-05-10 17:30:26 +0900544 tomoyo_put_name_union(&e.name1);
545 tomoyo_put_name_union(&e.name2);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900546 return error;
547}
548
549/**
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900550 * tomoyo_path_permission - Check permission for single path operation.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900551 *
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900552 * @r: Pointer to "struct tomoyo_request_info".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900553 * @operation: Type of operation.
554 * @filename: Filename to check.
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900555 *
556 * Returns 0 on success, negative value otherwise.
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900557 *
558 * Caller holds tomoyo_read_lock().
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900559 */
Tetsuo Handa05336de2010-06-16 16:20:24 +0900560int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
561 const struct tomoyo_path_info *filename)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900562{
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900563 int error;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900564
Tetsuo Handa57c25902010-06-03 20:38:44 +0900565 r->type = tomoyo_p2mac[operation];
Tetsuo Handabd03a3e2011-06-26 23:19:52 +0900566 r->mode = tomoyo_get_mode(r->domain->ns, r->profile, r->type);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900567 if (r->mode == TOMOYO_CONFIG_DISABLED)
568 return 0;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900569 r->param_type = TOMOYO_TYPE_PATH_ACL;
570 r->param.path.filename = filename;
571 r->param.path.operation = operation;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900572 do {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900573 tomoyo_check_acl(r, tomoyo_check_path_acl);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900574 error = tomoyo_audit_path_log(r);
Tetsuo Handa05336de2010-06-16 16:20:24 +0900575 /*
576 * Do not retry for execute request, for alias may have
577 * changed.
578 */
579 } while (error == TOMOYO_RETRY_REQUEST &&
580 operation != TOMOYO_TYPE_EXECUTE);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900581 return error;
582}
583
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900584/**
585 * tomoyo_same_path_number_acl - Check for duplicated "struct tomoyo_path_number_acl" entry.
586 *
587 * @a: Pointer to "struct tomoyo_acl_info".
588 * @b: Pointer to "struct tomoyo_acl_info".
589 *
590 * Returns true if @a == @b except permission bits, false otherwise.
591 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900592static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
593 const struct tomoyo_acl_info *b)
594{
595 const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
596 head);
597 const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
598 head);
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900599 return tomoyo_same_name_union(&p1->name, &p2->name) &&
600 tomoyo_same_number_union(&p1->number, &p2->number);
Tetsuo Handa237ab452010-06-12 20:46:22 +0900601}
602
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900603/**
604 * tomoyo_merge_path_number_acl - Merge duplicated "struct tomoyo_path_number_acl" entry.
605 *
606 * @a: Pointer to "struct tomoyo_acl_info".
607 * @b: Pointer to "struct tomoyo_acl_info".
608 * @is_delete: True for @a &= ~@b, false for @a |= @b.
609 *
610 * Returns true if @a is empty, false otherwise.
611 */
Tetsuo Handa237ab452010-06-12 20:46:22 +0900612static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
613 struct tomoyo_acl_info *b,
614 const bool is_delete)
615{
616 u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
617 head)->perm;
618 u8 perm = *a_perm;
619 const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
620 ->perm;
621 if (is_delete)
622 perm &= ~b_perm;
623 else
624 perm |= b_perm;
625 *a_perm = perm;
626 return !perm;
627}
628
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900629/**
630 * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
631 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900632 * @perm: Permission.
633 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900634 *
635 * Returns 0 on success, negative value otherwise.
636 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900637static int tomoyo_update_path_number_acl(const u8 perm,
638 struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900639{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900640 struct tomoyo_path_number_acl e = {
641 .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900642 .perm = perm
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900643 };
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900644 int error;
645 if (!tomoyo_parse_name_union(param, &e.name) ||
646 !tomoyo_parse_number_union(param, &e.number))
647 error = -EINVAL;
648 else
649 error = tomoyo_update_domain(&e.head, sizeof(e), param,
650 tomoyo_same_path_number_acl,
651 tomoyo_merge_path_number_acl);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900652 tomoyo_put_name_union(&e.name);
653 tomoyo_put_number_union(&e.number);
654 return error;
655}
656
657/**
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900658 * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
659 *
660 * @type: Type of operation.
661 * @path: Pointer to "struct path".
662 * @number: Number.
663 *
664 * Returns 0 on success, negative value otherwise.
665 */
666int tomoyo_path_number_perm(const u8 type, struct path *path,
667 unsigned long number)
668{
669 struct tomoyo_request_info r;
670 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900671 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900672 int idx;
673
Tetsuo Handa57c25902010-06-03 20:38:44 +0900674 if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900675 == TOMOYO_CONFIG_DISABLED || !path->dentry)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900676 return 0;
677 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900678 if (!tomoyo_get_realpath(&buf, path))
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900679 goto out;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900680 if (type == TOMOYO_TYPE_MKDIR)
681 tomoyo_add_slash(&buf);
Tetsuo Handacb917cf2010-06-16 16:28:21 +0900682 r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
683 r.param.path_number.operation = type;
684 r.param.path_number.filename = &buf;
685 r.param.path_number.number = number;
686 do {
687 tomoyo_check_acl(&r, tomoyo_check_path_number_acl);
688 error = tomoyo_audit_path_number_log(&r);
689 } while (error == TOMOYO_RETRY_REQUEST);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900690 kfree(buf.name);
Tetsuo Handacb917cf2010-06-16 16:28:21 +0900691 out:
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900692 tomoyo_read_unlock(idx);
693 if (r.mode != TOMOYO_CONFIG_ENFORCING)
694 error = 0;
695 return error;
696}
697
698/**
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900699 * tomoyo_check_open_permission - Check permission for "read" and "write".
700 *
701 * @domain: Pointer to "struct tomoyo_domain_info".
702 * @path: Pointer to "struct path".
703 * @flag: Flags for open().
704 *
705 * Returns 0 on success, negative value otherwise.
706 */
707int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
708 struct path *path, const int flag)
709{
710 const u8 acc_mode = ACC_MODE(flag);
Tetsuo Handaeae61f32011-03-02 16:54:24 +0900711 int error = 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900712 struct tomoyo_path_info buf;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900713 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900714 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900715
Tetsuo Handa57c25902010-06-03 20:38:44 +0900716 buf.name = NULL;
717 r.mode = TOMOYO_CONFIG_DISABLED;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900718 idx = tomoyo_read_lock();
Tetsuo Handa7c759642011-06-26 23:15:31 +0900719 if (acc_mode &&
720 tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
Tetsuo Handa57c25902010-06-03 20:38:44 +0900721 != TOMOYO_CONFIG_DISABLED) {
722 if (!tomoyo_get_realpath(&buf, path)) {
723 error = -ENOMEM;
724 goto out;
725 }
Tetsuo Handa7c759642011-06-26 23:15:31 +0900726 if (acc_mode & MAY_READ)
727 error = tomoyo_path_permission(&r, TOMOYO_TYPE_READ,
Tetsuo Handa57c25902010-06-03 20:38:44 +0900728 &buf);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900729 if (!error && (acc_mode & MAY_WRITE))
730 error = tomoyo_path_permission(&r, (flag & O_APPEND) ?
731 TOMOYO_TYPE_APPEND :
732 TOMOYO_TYPE_WRITE,
733 &buf);
Tetsuo Handa57c25902010-06-03 20:38:44 +0900734 }
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900735 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900736 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900737 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900738 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900739 error = 0;
740 return error;
741}
742
743/**
Tetsuo Handa7c759642011-06-26 23:15:31 +0900744 * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "append", "chroot" and "unmount".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900745 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900746 * @operation: Type of operation.
747 * @path: Pointer to "struct path".
748 *
749 * Returns 0 on success, negative value otherwise.
750 */
Tetsuo Handa97d69312010-02-16 09:46:15 +0900751int tomoyo_path_perm(const u8 operation, struct path *path)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900752{
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900753 struct tomoyo_request_info r;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900754 int error;
755 struct tomoyo_path_info buf;
756 bool is_enforce;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900757 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900758
Tetsuo Handa57c25902010-06-03 20:38:44 +0900759 if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
760 == TOMOYO_CONFIG_DISABLED)
761 return 0;
Tetsuo Handa7c759642011-06-26 23:15:31 +0900762 is_enforce = (r.mode == TOMOYO_CONFIG_ENFORCING);
763 error = -ENOMEM;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900764 buf.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900765 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900766 if (!tomoyo_get_realpath(&buf, path))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900767 goto out;
768 switch (operation) {
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900769 case TOMOYO_TYPE_RMDIR:
770 case TOMOYO_TYPE_CHROOT:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900771 tomoyo_add_slash(&buf);
772 break;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900773 }
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900774 error = tomoyo_path_permission(&r, operation, &buf);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900775 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900776 kfree(buf.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900777 tomoyo_read_unlock(idx);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900778 if (!is_enforce)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900779 error = 0;
780 return error;
781}
782
783/**
Tetsuo Handa75093152010-06-16 16:23:55 +0900784 * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900785 *
786 * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
787 * @path: Pointer to "struct path".
788 * @mode: Create mode.
789 * @dev: Device number.
790 *
791 * Returns 0 on success, negative value otherwise.
792 */
Tetsuo Handa75093152010-06-16 16:23:55 +0900793int tomoyo_mkdev_perm(const u8 operation, struct path *path,
Tetsuo Handa0df7e8b2011-06-26 23:16:36 +0900794 const unsigned int mode, unsigned int dev)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900795{
796 struct tomoyo_request_info r;
797 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900798 struct tomoyo_path_info buf;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900799 int idx;
800
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900801 if (tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
Tetsuo Handa57c25902010-06-03 20:38:44 +0900802 == TOMOYO_CONFIG_DISABLED)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900803 return 0;
804 idx = tomoyo_read_lock();
805 error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900806 if (tomoyo_get_realpath(&buf, path)) {
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900807 dev = new_decode_dev(dev);
Tetsuo Handa75093152010-06-16 16:23:55 +0900808 r.param_type = TOMOYO_TYPE_MKDEV_ACL;
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900809 r.param.mkdev.filename = &buf;
810 r.param.mkdev.operation = operation;
811 r.param.mkdev.mode = mode;
812 r.param.mkdev.major = MAJOR(dev);
813 r.param.mkdev.minor = MINOR(dev);
Tetsuo Handa99a85252010-06-16 16:22:51 +0900814 tomoyo_check_acl(&r, tomoyo_check_mkdev_acl);
815 error = tomoyo_audit_mkdev_log(&r);
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900816 kfree(buf.name);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900817 }
818 tomoyo_read_unlock(idx);
819 if (r.mode != TOMOYO_CONFIG_ENFORCING)
820 error = 0;
821 return error;
822}
823
824/**
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900825 * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900826 *
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900827 * @operation: Type of operation.
828 * @path1: Pointer to "struct path".
829 * @path2: Pointer to "struct path".
830 *
831 * Returns 0 on success, negative value otherwise.
832 */
Tetsuo Handa97d69312010-02-16 09:46:15 +0900833int tomoyo_path2_perm(const u8 operation, struct path *path1,
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900834 struct path *path2)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900835{
836 int error = -ENOMEM;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900837 struct tomoyo_path_info buf1;
838 struct tomoyo_path_info buf2;
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900839 struct tomoyo_request_info r;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900840 int idx;
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900841
Tetsuo Handa5625f2e2011-06-26 23:20:23 +0900842 if (tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
Tetsuo Handa57c25902010-06-03 20:38:44 +0900843 == TOMOYO_CONFIG_DISABLED)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900844 return 0;
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900845 buf1.name = NULL;
846 buf2.name = NULL;
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900847 idx = tomoyo_read_lock();
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900848 if (!tomoyo_get_realpath(&buf1, path1) ||
849 !tomoyo_get_realpath(&buf2, path2))
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900850 goto out;
Tetsuo Handa57c25902010-06-03 20:38:44 +0900851 switch (operation) {
852 struct dentry *dentry;
853 case TOMOYO_TYPE_RENAME:
854 case TOMOYO_TYPE_LINK:
855 dentry = path1->dentry;
856 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
857 break;
858 /* fall through */
859 case TOMOYO_TYPE_PIVOT_ROOT:
860 tomoyo_add_slash(&buf1);
861 tomoyo_add_slash(&buf2);
862 break;
863 }
Tetsuo Handacf6e9a62010-06-16 16:21:36 +0900864 r.param_type = TOMOYO_TYPE_PATH2_ACL;
865 r.param.path2.operation = operation;
866 r.param.path2.filename1 = &buf1;
867 r.param.path2.filename2 = &buf2;
Tetsuo Handa17fcfbd2010-05-17 10:11:36 +0900868 do {
Tetsuo Handa99a85252010-06-16 16:22:51 +0900869 tomoyo_check_acl(&r, tomoyo_check_path2_acl);
870 error = tomoyo_audit_path2_log(&r);
871 } while (error == TOMOYO_RETRY_REQUEST);
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900872 out:
Tetsuo Handac8c57e82010-06-03 20:36:43 +0900873 kfree(buf1.name);
874 kfree(buf2.name);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900875 tomoyo_read_unlock(idx);
Tetsuo Handacb0abe62010-05-17 10:08:05 +0900876 if (r.mode != TOMOYO_CONFIG_ENFORCING)
Kentaro Takedab69a54e2009-02-05 17:18:14 +0900877 error = 0;
878 return error;
879}
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900880
881/**
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900882 * tomoyo_same_mount_acl - Check for duplicated "struct tomoyo_mount_acl" entry.
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900883 *
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900884 * @a: Pointer to "struct tomoyo_acl_info".
885 * @b: Pointer to "struct tomoyo_acl_info".
886 *
887 * Returns true if @a == @b, false otherwise.
888 */
889static bool tomoyo_same_mount_acl(const struct tomoyo_acl_info *a,
890 const struct tomoyo_acl_info *b)
891{
892 const struct tomoyo_mount_acl *p1 = container_of(a, typeof(*p1), head);
893 const struct tomoyo_mount_acl *p2 = container_of(b, typeof(*p2), head);
894 return tomoyo_same_name_union(&p1->dev_name, &p2->dev_name) &&
895 tomoyo_same_name_union(&p1->dir_name, &p2->dir_name) &&
896 tomoyo_same_name_union(&p1->fs_type, &p2->fs_type) &&
897 tomoyo_same_number_union(&p1->flags, &p2->flags);
898}
899
900/**
901 * tomoyo_update_mount_acl - Write "struct tomoyo_mount_acl" list.
902 *
903 * @param: Pointer to "struct tomoyo_acl_param".
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900904 *
905 * Returns 0 on success, negative value otherwise.
906 *
907 * Caller holds tomoyo_read_lock().
908 */
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900909static int tomoyo_update_mount_acl(struct tomoyo_acl_param *param)
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900910{
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900911 struct tomoyo_mount_acl e = { .head.type = TOMOYO_TYPE_MOUNT_ACL };
912 int error;
913 if (!tomoyo_parse_name_union(param, &e.dev_name) ||
914 !tomoyo_parse_name_union(param, &e.dir_name) ||
915 !tomoyo_parse_name_union(param, &e.fs_type) ||
916 !tomoyo_parse_number_union(param, &e.flags))
917 error = -EINVAL;
918 else
919 error = tomoyo_update_domain(&e.head, sizeof(e), param,
920 tomoyo_same_mount_acl, NULL);
921 tomoyo_put_name_union(&e.dev_name);
922 tomoyo_put_name_union(&e.dir_name);
923 tomoyo_put_name_union(&e.fs_type);
924 tomoyo_put_number_union(&e.flags);
925 return error;
926}
927
928/**
929 * tomoyo_write_file - Update file related list.
930 *
931 * @param: Pointer to "struct tomoyo_acl_param".
932 *
933 * Returns 0 on success, negative value otherwise.
934 *
935 * Caller holds tomoyo_read_lock().
936 */
937int tomoyo_write_file(struct tomoyo_acl_param *param)
938{
939 u16 perm = 0;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900940 u8 type;
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900941 const char *operation = tomoyo_read_token(param);
942 for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++)
943 if (tomoyo_permstr(operation, tomoyo_path_keyword[type]))
944 perm |= 1 << type;
945 if (perm)
946 return tomoyo_update_path_acl(perm, param);
947 for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++)
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900948 if (tomoyo_permstr(operation,
949 tomoyo_mac_keywords[tomoyo_pp2mac[type]]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900950 perm |= 1 << type;
951 if (perm)
952 return tomoyo_update_path2_acl(perm, param);
953 for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++)
954 if (tomoyo_permstr(operation,
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900955 tomoyo_mac_keywords[tomoyo_pn2mac[type]]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900956 perm |= 1 << type;
957 if (perm)
958 return tomoyo_update_path_number_acl(perm, param);
959 for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++)
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900960 if (tomoyo_permstr(operation,
961 tomoyo_mac_keywords[tomoyo_pnnn2mac[type]]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900962 perm |= 1 << type;
963 if (perm)
964 return tomoyo_update_mkdev_acl(perm, param);
Tetsuo Handa2c47ab92011-06-26 23:21:19 +0900965 if (tomoyo_permstr(operation,
966 tomoyo_mac_keywords[TOMOYO_MAC_FILE_MOUNT]))
Tetsuo Handaa238cf52011-06-26 23:17:10 +0900967 return tomoyo_update_mount_acl(param);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900968 return -EINVAL;
969}