TOMOYO: Update kernel-doc.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / security / tomoyo / tomoyo.c
CommitLineData
f7433243
KT
1/*
2 * security/tomoyo/tomoyo.c
3 *
0f2a55d5 4 * Copyright (C) 2005-2011 NTT DATA CORPORATION
f7433243
KT
5 */
6
7#include <linux/security.h>
8#include "common.h"
f7433243 9
0f2a55d5
TH
10/**
11 * tomoyo_cred_alloc_blank - Target for security_cred_alloc_blank().
12 *
13 * @new: Pointer to "struct cred".
14 * @gfp: Memory allocation flags.
15 *
16 * Returns 0.
17 */
ee18d64c
DH
18static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
19{
20 new->security = NULL;
21 return 0;
22}
23
0f2a55d5
TH
24/**
25 * tomoyo_cred_prepare - Target for security_prepare_creds().
26 *
27 * @new: Pointer to "struct cred".
28 * @old: Pointer to "struct cred".
29 * @gfp: Memory allocation flags.
30 *
31 * Returns 0.
32 */
f7433243
KT
33static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
34 gfp_t gfp)
35{
ec8e6a4e
TH
36 struct tomoyo_domain_info *domain = old->security;
37 new->security = domain;
38 if (domain)
39 atomic_inc(&domain->users);
f7433243
KT
40 return 0;
41}
42
0f2a55d5
TH
43/**
44 * tomoyo_cred_transfer - Target for security_transfer_creds().
45 *
46 * @new: Pointer to "struct cred".
47 * @old: Pointer to "struct cred".
48 */
ee18d64c
DH
49static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
50{
ec8e6a4e
TH
51 tomoyo_cred_prepare(new, old, 0);
52}
53
0f2a55d5
TH
54/**
55 * tomoyo_cred_free - Target for security_cred_free().
56 *
57 * @cred: Pointer to "struct cred".
58 */
ec8e6a4e
TH
59static void tomoyo_cred_free(struct cred *cred)
60{
61 struct tomoyo_domain_info *domain = cred->security;
62 if (domain)
63 atomic_dec(&domain->users);
ee18d64c
DH
64}
65
0f2a55d5
TH
66/**
67 * tomoyo_bprm_set_creds - Target for security_bprm_set_creds().
68 *
69 * @bprm: Pointer to "struct linux_binprm".
70 *
71 * Returns 0 on success, negative value otherwise.
72 */
f7433243
KT
73static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
74{
b1338d19
HRK
75 int rc;
76
77 rc = cap_bprm_set_creds(bprm);
78 if (rc)
79 return rc;
80
f7433243
KT
81 /*
82 * Do only if this function is called for the first time of an execve
83 * operation.
84 */
85 if (bprm->cred_prepared)
86 return 0;
7986cf28 87#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
f7433243
KT
88 /*
89 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
90 * for the first time.
91 */
92 if (!tomoyo_policy_loaded)
93 tomoyo_load_policy(bprm->filename);
7986cf28 94#endif
ec8e6a4e
TH
95 /*
96 * Release reference to "struct tomoyo_domain_info" stored inside
97 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
98 * stored inside "bprm->cred->security" will be acquired later inside
99 * tomoyo_find_next_domain().
100 */
101 atomic_dec(&((struct tomoyo_domain_info *)
102 bprm->cred->security)->users);
f7433243
KT
103 /*
104 * Tell tomoyo_bprm_check_security() is called for the first time of an
105 * execve operation.
106 */
107 bprm->cred->security = NULL;
108 return 0;
109}
110
0f2a55d5
TH
111/**
112 * tomoyo_bprm_check_security - Target for security_bprm_check().
113 *
114 * @bprm: Pointer to "struct linux_binprm".
115 *
116 * Returns 0 on success, negative value otherwise.
117 */
f7433243
KT
118static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
119{
120 struct tomoyo_domain_info *domain = bprm->cred->security;
121
122 /*
123 * Execute permission is checked against pathname passed to do_execve()
124 * using current domain.
125 */
fdb8ebb7 126 if (!domain) {
fdb8ebb7
TH
127 const int idx = tomoyo_read_lock();
128 const int err = tomoyo_find_next_domain(bprm);
129 tomoyo_read_unlock(idx);
130 return err;
131 }
f7433243
KT
132 /*
133 * Read permission is checked against interpreters using next domain.
f7433243 134 */
0f2a55d5
TH
135 return tomoyo_check_open_permission(domain, &bprm->file->f_path,
136 O_RDONLY);
f7433243
KT
137}
138
0f2a55d5
TH
139/**
140 * tomoyo_inode_getattr - Target for security_inode_getattr().
141 *
142 * @mnt: Pointer to "struct vfsmount".
143 * @dentry: Pointer to "struct dentry".
144 *
145 * Returns 0 on success, negative value otherwise.
146 */
7c75964f
TH
147static int tomoyo_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
148{
149 struct path path = { mnt, dentry };
97fb35e4 150 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, &path, NULL);
7c75964f
TH
151}
152
0f2a55d5
TH
153/**
154 * tomoyo_path_truncate - Target for security_path_truncate().
155 *
156 * @path: Pointer to "struct path".
157 *
158 * Returns 0 on success, negative value otherwise.
159 */
ea0d3ab2 160static int tomoyo_path_truncate(struct path *path)
f7433243 161{
97fb35e4 162 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
f7433243
KT
163}
164
0f2a55d5
TH
165/**
166 * tomoyo_path_unlink - Target for security_path_unlink().
167 *
168 * @parent: Pointer to "struct path".
169 * @dentry: Pointer to "struct dentry".
170 *
171 * Returns 0 on success, negative value otherwise.
172 */
f7433243
KT
173static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
174{
175 struct path path = { parent->mnt, dentry };
97fb35e4 176 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
f7433243
KT
177}
178
0f2a55d5
TH
179/**
180 * tomoyo_path_mkdir - Target for security_path_mkdir().
181 *
182 * @parent: Pointer to "struct path".
183 * @dentry: Pointer to "struct dentry".
184 * @mode: DAC permission mode.
185 *
186 * Returns 0 on success, negative value otherwise.
187 */
f7433243
KT
188static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
189 int mode)
190{
191 struct path path = { parent->mnt, dentry };
a1f9bb6a
TH
192 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
193 mode & S_IALLUGO);
f7433243
KT
194}
195
0f2a55d5
TH
196/**
197 * tomoyo_path_rmdir - Target for security_path_rmdir().
198 *
199 * @parent: Pointer to "struct path".
200 * @dentry: Pointer to "struct dentry".
201 *
202 * Returns 0 on success, negative value otherwise.
203 */
f7433243
KT
204static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
205{
206 struct path path = { parent->mnt, dentry };
97fb35e4 207 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
f7433243
KT
208}
209
0f2a55d5
TH
210/**
211 * tomoyo_path_symlink - Target for security_path_symlink().
212 *
213 * @parent: Pointer to "struct path".
214 * @dentry: Pointer to "struct dentry".
215 * @old_name: Symlink's content.
216 *
217 * Returns 0 on success, negative value otherwise.
218 */
f7433243
KT
219static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
220 const char *old_name)
221{
222 struct path path = { parent->mnt, dentry };
97fb35e4 223 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
f7433243
KT
224}
225
0f2a55d5
TH
226/**
227 * tomoyo_path_mknod - Target for security_path_mknod().
228 *
229 * @parent: Pointer to "struct path".
230 * @dentry: Pointer to "struct dentry".
231 * @mode: DAC permission mode.
232 * @dev: Device attributes.
233 *
234 * Returns 0 on success, negative value otherwise.
235 */
f7433243
KT
236static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
237 int mode, unsigned int dev)
238{
239 struct path path = { parent->mnt, dentry };
7ef61233 240 int type = TOMOYO_TYPE_CREATE;
a1f9bb6a 241 const unsigned int perm = mode & S_IALLUGO;
f7433243
KT
242
243 switch (mode & S_IFMT) {
244 case S_IFCHR:
7ef61233 245 type = TOMOYO_TYPE_MKCHAR;
f7433243
KT
246 break;
247 case S_IFBLK:
7ef61233 248 type = TOMOYO_TYPE_MKBLOCK;
f7433243 249 break;
a1f9bb6a
TH
250 default:
251 goto no_dev;
252 }
75093152 253 return tomoyo_mkdev_perm(type, &path, perm, dev);
a1f9bb6a
TH
254 no_dev:
255 switch (mode & S_IFMT) {
f7433243 256 case S_IFIFO:
7ef61233 257 type = TOMOYO_TYPE_MKFIFO;
f7433243
KT
258 break;
259 case S_IFSOCK:
7ef61233 260 type = TOMOYO_TYPE_MKSOCK;
f7433243
KT
261 break;
262 }
a1f9bb6a 263 return tomoyo_path_number_perm(type, &path, perm);
f7433243
KT
264}
265
0f2a55d5
TH
266/**
267 * tomoyo_path_link - Target for security_path_link().
268 *
269 * @old_dentry: Pointer to "struct dentry".
270 * @new_dir: Pointer to "struct path".
271 * @new_dentry: Pointer to "struct dentry".
272 *
273 * Returns 0 on success, negative value otherwise.
274 */
f7433243
KT
275static int tomoyo_path_link(struct dentry *old_dentry, struct path *new_dir,
276 struct dentry *new_dentry)
277{
278 struct path path1 = { new_dir->mnt, old_dentry };
279 struct path path2 = { new_dir->mnt, new_dentry };
97d6931e 280 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
f7433243
KT
281}
282
0f2a55d5
TH
283/**
284 * tomoyo_path_rename - Target for security_path_rename().
285 *
286 * @old_parent: Pointer to "struct path".
287 * @old_dentry: Pointer to "struct dentry".
288 * @new_parent: Pointer to "struct path".
289 * @new_dentry: Pointer to "struct dentry".
290 *
291 * Returns 0 on success, negative value otherwise.
292 */
f7433243
KT
293static int tomoyo_path_rename(struct path *old_parent,
294 struct dentry *old_dentry,
295 struct path *new_parent,
296 struct dentry *new_dentry)
297{
298 struct path path1 = { old_parent->mnt, old_dentry };
299 struct path path2 = { new_parent->mnt, new_dentry };
97d6931e 300 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
f7433243
KT
301}
302
0f2a55d5
TH
303/**
304 * tomoyo_file_fcntl - Target for security_file_fcntl().
305 *
306 * @file: Pointer to "struct file".
307 * @cmd: Command for fcntl().
308 * @arg: Argument for @cmd.
309 *
310 * Returns 0 on success, negative value otherwise.
311 */
f7433243
KT
312static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
313 unsigned long arg)
314{
7c75964f
TH
315 if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
316 return 0;
317 return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
318 O_WRONLY | (arg & O_APPEND));
f7433243
KT
319}
320
0f2a55d5
TH
321/**
322 * tomoyo_dentry_open - Target for security_dentry_open().
323 *
324 * @f: Pointer to "struct file".
325 * @cred: Pointer to "struct cred".
326 *
327 * Returns 0 on success, negative value otherwise.
328 */
f7433243
KT
329static int tomoyo_dentry_open(struct file *f, const struct cred *cred)
330{
331 int flags = f->f_flags;
f7433243
KT
332 /* Don't check read permission here if called from do_execve(). */
333 if (current->in_execve)
334 return 0;
335 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
336}
337
0f2a55d5
TH
338/**
339 * tomoyo_file_ioctl - Target for security_file_ioctl().
340 *
341 * @file: Pointer to "struct file".
342 * @cmd: Command for ioctl().
343 * @arg: Argument for @cmd.
344 *
345 * Returns 0 on success, negative value otherwise.
346 */
937bf613
TH
347static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
348 unsigned long arg)
349{
a1f9bb6a 350 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
937bf613
TH
351}
352
0f2a55d5
TH
353/**
354 * tomoyo_path_chmod - Target for security_path_chmod().
355 *
356 * @dentry: Pointer to "struct dentry".
357 * @mnt: Pointer to "struct vfsmount".
358 * @mode: DAC permission mode.
359 *
360 * Returns 0 on success, negative value otherwise.
361 */
937bf613
TH
362static int tomoyo_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
363 mode_t mode)
364{
365 struct path path = { mnt, dentry };
a1f9bb6a
TH
366 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, &path,
367 mode & S_IALLUGO);
937bf613
TH
368}
369
0f2a55d5
TH
370/**
371 * tomoyo_path_chown - Target for security_path_chown().
372 *
373 * @path: Pointer to "struct path".
374 * @uid: Owner ID.
375 * @gid: Group ID.
376 *
377 * Returns 0 on success, negative value otherwise.
378 */
937bf613
TH
379static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
380{
381 int error = 0;
382 if (uid != (uid_t) -1)
a1f9bb6a 383 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, uid);
937bf613 384 if (!error && gid != (gid_t) -1)
a1f9bb6a 385 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, gid);
937bf613
TH
386 return error;
387}
388
0f2a55d5
TH
389/**
390 * tomoyo_path_chroot - Target for security_path_chroot().
391 *
392 * @path: Pointer to "struct path".
393 *
394 * Returns 0 on success, negative value otherwise.
395 */
937bf613
TH
396static int tomoyo_path_chroot(struct path *path)
397{
97fb35e4 398 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
937bf613
TH
399}
400
0f2a55d5
TH
401/**
402 * tomoyo_sb_mount - Target for security_sb_mount().
403 *
404 * @dev_name: Name of device file. Maybe NULL.
405 * @path: Pointer to "struct path".
406 * @type: Name of filesystem type. Maybe NULL.
407 * @flags: Mount options.
408 * @data: Optional data. Maybe NULL.
409 *
410 * Returns 0 on success, negative value otherwise.
411 */
937bf613
TH
412static int tomoyo_sb_mount(char *dev_name, struct path *path,
413 char *type, unsigned long flags, void *data)
414{
2106ccd9 415 return tomoyo_mount_permission(dev_name, path, type, flags, data);
937bf613
TH
416}
417
0f2a55d5
TH
418/**
419 * tomoyo_sb_umount - Target for security_sb_umount().
420 *
421 * @mnt: Pointer to "struct vfsmount".
422 * @flags: Unmount options.
423 *
424 * Returns 0 on success, negative value otherwise.
425 */
937bf613
TH
426static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
427{
428 struct path path = { mnt, mnt->mnt_root };
97fb35e4 429 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
937bf613
TH
430}
431
0f2a55d5
TH
432/**
433 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
434 *
435 * @old_path: Pointer to "struct path".
436 * @new_path: Pointer to "struct path".
437 *
438 * Returns 0 on success, negative value otherwise.
439 */
937bf613
TH
440static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)
441{
97d6931e 442 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
937bf613
TH
443}
444
c3fa109a
TH
445/*
446 * tomoyo_security_ops is a "struct security_operations" which is used for
447 * registering TOMOYO.
448 */
f7433243
KT
449static struct security_operations tomoyo_security_ops = {
450 .name = "tomoyo",
ee18d64c 451 .cred_alloc_blank = tomoyo_cred_alloc_blank,
f7433243 452 .cred_prepare = tomoyo_cred_prepare,
ee18d64c 453 .cred_transfer = tomoyo_cred_transfer,
ec8e6a4e 454 .cred_free = tomoyo_cred_free,
f7433243
KT
455 .bprm_set_creds = tomoyo_bprm_set_creds,
456 .bprm_check_security = tomoyo_bprm_check_security,
f7433243
KT
457 .file_fcntl = tomoyo_file_fcntl,
458 .dentry_open = tomoyo_dentry_open,
459 .path_truncate = tomoyo_path_truncate,
460 .path_unlink = tomoyo_path_unlink,
461 .path_mkdir = tomoyo_path_mkdir,
462 .path_rmdir = tomoyo_path_rmdir,
463 .path_symlink = tomoyo_path_symlink,
464 .path_mknod = tomoyo_path_mknod,
465 .path_link = tomoyo_path_link,
466 .path_rename = tomoyo_path_rename,
7c75964f 467 .inode_getattr = tomoyo_inode_getattr,
937bf613
TH
468 .file_ioctl = tomoyo_file_ioctl,
469 .path_chmod = tomoyo_path_chmod,
470 .path_chown = tomoyo_path_chown,
471 .path_chroot = tomoyo_path_chroot,
472 .sb_mount = tomoyo_sb_mount,
473 .sb_umount = tomoyo_sb_umount,
474 .sb_pivotroot = tomoyo_sb_pivotroot,
f7433243
KT
475};
476
fdb8ebb7
TH
477/* Lock for GC. */
478struct srcu_struct tomoyo_ss;
479
0f2a55d5
TH
480/**
481 * tomoyo_init - Register TOMOYO Linux as a LSM module.
482 *
483 * Returns 0.
484 */
f7433243
KT
485static int __init tomoyo_init(void)
486{
487 struct cred *cred = (struct cred *) current_cred();
488
489 if (!security_module_enable(&tomoyo_security_ops))
490 return 0;
491 /* register ourselves with the security framework */
fdb8ebb7
TH
492 if (register_security(&tomoyo_security_ops) ||
493 init_srcu_struct(&tomoyo_ss))
f7433243
KT
494 panic("Failure registering TOMOYO Linux");
495 printk(KERN_INFO "TOMOYO Linux initialized\n");
496 cred->security = &tomoyo_kernel_domain;
c3ef1500 497 tomoyo_mm_init();
f7433243
KT
498 return 0;
499}
500
501security_initcall(tomoyo_init);