blob: 9f932e2d68521c2da50be376cb41c51d700425f4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kentaro Takedaf7433242009-02-05 17:18:16 +09002/*
3 * security/tomoyo/tomoyo.c
4 *
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +09005 * Copyright (C) 2005-2011 NTT DATA CORPORATION
Kentaro Takedaf7433242009-02-05 17:18:16 +09006 */
7
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -07008#include <linux/lsm_hooks.h>
Kentaro Takedaf7433242009-02-05 17:18:16 +09009#include "common.h"
Kentaro Takedaf7433242009-02-05 17:18:16 +090010
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090011/**
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 */
David Howellsee18d642009-09-02 09:14:21 +010019static int tomoyo_cred_alloc_blank(struct cred *new, gfp_t gfp)
20{
21 new->security = NULL;
22 return 0;
23}
24
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090025/**
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 */
Kentaro Takedaf7433242009-02-05 17:18:16 +090034static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
35 gfp_t gfp)
36{
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090037 struct tomoyo_domain_info *domain = old->security;
38 new->security = domain;
39 if (domain)
40 atomic_inc(&domain->users);
Kentaro Takedaf7433242009-02-05 17:18:16 +090041 return 0;
42}
43
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090044/**
45 * tomoyo_cred_transfer - Target for security_transfer_creds().
46 *
47 * @new: Pointer to "struct cred".
48 * @old: Pointer to "struct cred".
49 */
David Howellsee18d642009-09-02 09:14:21 +010050static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
51{
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090052 tomoyo_cred_prepare(new, old, 0);
53}
54
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090055/**
56 * tomoyo_cred_free - Target for security_cred_free().
57 *
58 * @cred: Pointer to "struct cred".
59 */
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090060static void tomoyo_cred_free(struct cred *cred)
61{
62 struct tomoyo_domain_info *domain = cred->security;
63 if (domain)
64 atomic_dec(&domain->users);
David Howellsee18d642009-09-02 09:14:21 +010065}
66
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090067/**
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 */
Kentaro Takedaf7433242009-02-05 17:18:16 +090074static 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 */
Kees Cookddb4a142017-07-18 15:25:23 -070080 if (bprm->called_set_creds)
Kentaro Takedaf7433242009-02-05 17:18:16 +090081 return 0;
Tetsuo Handa7986cf22011-06-29 13:07:52 +090082#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
Kentaro Takedaf7433242009-02-05 17:18:16 +090083 /*
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);
Tetsuo Handa7986cf22011-06-29 13:07:52 +090089#endif
Kentaro Takedaf7433242009-02-05 17:18:16 +090090 /*
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090091 * 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);
98 /*
Kentaro Takedaf7433242009-02-05 17:18:16 +090099 * 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
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900106/**
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 */
Kentaro Takedaf7433242009-02-05 17:18:16 +0900113static 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 */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900121 if (!domain) {
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900122 const int idx = tomoyo_read_lock();
123 const int err = tomoyo_find_next_domain(bprm);
124 tomoyo_read_unlock(idx);
125 return err;
126 }
Kentaro Takedaf7433242009-02-05 17:18:16 +0900127 /*
128 * Read permission is checked against interpreters using next domain.
Kentaro Takedaf7433242009-02-05 17:18:16 +0900129 */
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900130 return tomoyo_check_open_permission(domain, &bprm->file->f_path,
131 O_RDONLY);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900132}
133
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900134/**
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 */
Al Viro3f7036a2015-03-08 19:28:30 -0400142static int tomoyo_inode_getattr(const struct path *path)
Tetsuo Handa7c759642011-06-26 23:15:31 +0900143{
Al Viro3f7036a2015-03-08 19:28:30 -0400144 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900145}
146
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900147/**
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 */
Al Viro81f4c502016-03-25 14:22:01 -0400154static int tomoyo_path_truncate(const struct path *path)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900155{
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900156 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900157}
158
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900159/**
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 */
Al Viro989f74e2016-03-25 15:13:39 -0400167static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900168{
Kees Cook82917982017-03-29 16:52:58 -0700169 struct path path = { .mnt = parent->mnt, .dentry = dentry };
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900170 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900171}
172
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900173/**
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 */
Al Virod3607752016-03-25 15:21:09 -0400182static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry,
Al Viro4572bef2011-11-21 14:56:21 -0500183 umode_t mode)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900184{
Kees Cook82917982017-03-29 16:52:58 -0700185 struct path path = { .mnt = parent->mnt, .dentry = dentry };
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900186 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
187 mode & S_IALLUGO);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900188}
189
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900190/**
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 */
Al Viro989f74e2016-03-25 15:13:39 -0400198static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900199{
Kees Cook82917982017-03-29 16:52:58 -0700200 struct path path = { .mnt = parent->mnt, .dentry = dentry };
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900201 return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900202}
203
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900204/**
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 */
Al Virod3607752016-03-25 15:21:09 -0400213static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900214 const char *old_name)
215{
Kees Cook82917982017-03-29 16:52:58 -0700216 struct path path = { .mnt = parent->mnt, .dentry = dentry };
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900217 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900218}
219
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900220/**
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 */
Al Virod3607752016-03-25 15:21:09 -0400230static int tomoyo_path_mknod(const struct path *parent, struct dentry *dentry,
Al Viro04fc66e2011-11-21 14:58:38 -0500231 umode_t mode, unsigned int dev)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900232{
Kees Cook82917982017-03-29 16:52:58 -0700233 struct path path = { .mnt = parent->mnt, .dentry = dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900234 int type = TOMOYO_TYPE_CREATE;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900235 const unsigned int perm = mode & S_IALLUGO;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900236
237 switch (mode & S_IFMT) {
238 case S_IFCHR:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900239 type = TOMOYO_TYPE_MKCHAR;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900240 break;
241 case S_IFBLK:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900242 type = TOMOYO_TYPE_MKBLOCK;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900243 break;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900244 default:
245 goto no_dev;
246 }
Tetsuo Handa75093152010-06-16 16:23:55 +0900247 return tomoyo_mkdev_perm(type, &path, perm, dev);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900248 no_dev:
249 switch (mode & S_IFMT) {
Kentaro Takedaf7433242009-02-05 17:18:16 +0900250 case S_IFIFO:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900251 type = TOMOYO_TYPE_MKFIFO;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900252 break;
253 case S_IFSOCK:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900254 type = TOMOYO_TYPE_MKSOCK;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900255 break;
256 }
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900257 return tomoyo_path_number_perm(type, &path, perm);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900258}
259
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900260/**
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 */
Al Viro3ccee462016-03-25 15:27:45 -0400269static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_dir,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900270 struct dentry *new_dentry)
271{
Kees Cook82917982017-03-29 16:52:58 -0700272 struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry };
273 struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900274 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900275}
276
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900277/**
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 */
Al Viro3ccee462016-03-25 15:27:45 -0400287static int tomoyo_path_rename(const struct path *old_parent,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900288 struct dentry *old_dentry,
Al Viro3ccee462016-03-25 15:27:45 -0400289 const struct path *new_parent,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900290 struct dentry *new_dentry)
291{
Kees Cook82917982017-03-29 16:52:58 -0700292 struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry };
293 struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900294 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900295}
296
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900297/**
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 */
Kentaro Takedaf7433242009-02-05 17:18:16 +0900306static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
307 unsigned long arg)
308{
Tetsuo Handa7c759642011-06-26 23:15:31 +0900309 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));
Kentaro Takedaf7433242009-02-05 17:18:16 +0900313}
314
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900315/**
Eric Paris83d49852012-04-04 13:45:40 -0400316 * tomoyo_file_open - Target for security_file_open().
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900317 *
318 * @f: Pointer to "struct file".
319 * @cred: Pointer to "struct cred".
320 *
321 * Returns 0 on success, negative value otherwise.
322 */
Al Viro94817692018-07-10 14:13:18 -0400323static int tomoyo_file_open(struct file *f)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900324{
325 int flags = f->f_flags;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900326 /* 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
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900332/**
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 */
Tetsuo Handa937bf612009-12-02 21:09:48 +0900341static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
342 unsigned long arg)
343{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900344 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900345}
346
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900347/**
348 * tomoyo_path_chmod - Target for security_path_chmod().
349 *
Al Virocdcf1162011-12-08 10:51:53 -0500350 * @path: Pointer to "struct path".
351 * @mode: DAC permission mode.
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900352 *
353 * Returns 0 on success, negative value otherwise.
354 */
Al Virobe01f9f2016-03-25 14:56:23 -0400355static int tomoyo_path_chmod(const struct path *path, umode_t mode)
Tetsuo Handa937bf612009-12-02 21:09:48 +0900356{
Al Virocdcf1162011-12-08 10:51:53 -0500357 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900358 mode & S_IALLUGO);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900359}
360
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900361/**
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 */
Al Viro7fd25da2016-03-25 14:44:41 -0400370static int tomoyo_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
Tetsuo Handa937bf612009-12-02 21:09:48 +0900371{
372 int error = 0;
Eric W. Biedermand2b31ca2012-06-01 16:14:19 -0600373 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));
Tetsuo Handa937bf612009-12-02 21:09:48 +0900379 return error;
380}
381
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900382/**
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 */
Al Viro77b286c2016-03-25 15:28:43 -0400389static int tomoyo_path_chroot(const struct path *path)
Tetsuo Handa937bf612009-12-02 21:09:48 +0900390{
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900391 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900392}
393
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900394/**
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 */
Al Viro8a04c432016-03-25 14:52:53 -0400405static int tomoyo_sb_mount(const char *dev_name, const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -0400406 const char *type, unsigned long flags, void *data)
Tetsuo Handa937bf612009-12-02 21:09:48 +0900407{
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900408 return tomoyo_mount_permission(dev_name, path, type, flags, data);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900409}
410
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900411/**
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 */
Tetsuo Handa937bf612009-12-02 21:09:48 +0900419static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
420{
Kees Cook82917982017-03-29 16:52:58 -0700421 struct path path = { .mnt = mnt, .dentry = mnt->mnt_root };
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900422 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900423}
424
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900425/**
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 */
Al Viro3b73b682016-03-25 15:31:19 -0400433static int tomoyo_sb_pivotroot(const struct path *old_path, const struct path *new_path)
Tetsuo Handa937bf612009-12-02 21:09:48 +0900434{
Tetsuo Handa97d69312010-02-16 09:46:15 +0900435 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900436}
437
Tetsuo Handa059d84d2011-09-10 15:23:54 +0900438/**
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
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900496/*
497 * tomoyo_security_ops is a "struct security_operations" which is used for
498 * registering TOMOYO.
499 */
James Morrisca97d932017-02-15 00:18:51 +1100500static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -0700501 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),
Kentaro Takedaf7433242009-02-05 17:18:16 +0900529};
530
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900531/* Lock for GC. */
Lai Jiangshan505f14f2013-03-16 00:50:53 +0800532DEFINE_SRCU(tomoyo_ss);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900533
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900534/**
535 * tomoyo_init - Register TOMOYO Linux as a LSM module.
536 *
537 * Returns 0.
538 */
Kentaro Takedaf7433242009-02-05 17:18:16 +0900539static int __init tomoyo_init(void)
540{
541 struct cred *cred = (struct cred *) current_cred();
542
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700543 if (!security_module_enable("tomoyo"))
Kentaro Takedaf7433242009-02-05 17:18:16 +0900544 return 0;
545 /* register ourselves with the security framework */
Casey Schauflerd69dece52017-01-18 17:09:05 -0800546 security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo");
Kentaro Takedaf7433242009-02-05 17:18:16 +0900547 printk(KERN_INFO "TOMOYO Linux initialized\n");
548 cred->security = &tomoyo_kernel_domain;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900549 tomoyo_mm_init();
Kentaro Takedaf7433242009-02-05 17:18:16 +0900550 return 0;
551}
552
553security_initcall(tomoyo_init);