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