blob: 9094cf41a2478c360baf458d6ea213578fe211d0 [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{
Casey Schaufler43fc4602018-09-21 17:18:07 -070021 struct tomoyo_domain_info **blob = tomoyo_cred(new);
22
23 *blob = NULL;
David Howellsee18d642009-09-02 09:14:21 +010024 return 0;
25}
26
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090027/**
28 * tomoyo_cred_prepare - Target for security_prepare_creds().
29 *
30 * @new: Pointer to "struct cred".
31 * @old: Pointer to "struct cred".
32 * @gfp: Memory allocation flags.
33 *
34 * Returns 0.
35 */
Kentaro Takedaf7433242009-02-05 17:18:16 +090036static int tomoyo_cred_prepare(struct cred *new, const struct cred *old,
37 gfp_t gfp)
38{
Casey Schaufler43fc4602018-09-21 17:18:07 -070039 struct tomoyo_domain_info **old_blob = tomoyo_cred(old);
40 struct tomoyo_domain_info **new_blob = tomoyo_cred(new);
41 struct tomoyo_domain_info *domain;
42
43 domain = *old_blob;
44 *new_blob = domain;
45
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090046 if (domain)
47 atomic_inc(&domain->users);
Kentaro Takedaf7433242009-02-05 17:18:16 +090048 return 0;
49}
50
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090051/**
52 * tomoyo_cred_transfer - Target for security_transfer_creds().
53 *
54 * @new: Pointer to "struct cred".
55 * @old: Pointer to "struct cred".
56 */
David Howellsee18d642009-09-02 09:14:21 +010057static void tomoyo_cred_transfer(struct cred *new, const struct cred *old)
58{
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090059 tomoyo_cred_prepare(new, old, 0);
60}
61
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090062/**
63 * tomoyo_cred_free - Target for security_cred_free().
64 *
65 * @cred: Pointer to "struct cred".
66 */
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090067static void tomoyo_cred_free(struct cred *cred)
68{
Casey Schaufler43fc4602018-09-21 17:18:07 -070069 struct tomoyo_domain_info **blob = tomoyo_cred(cred);
70 struct tomoyo_domain_info *domain = *blob;
71
Tetsuo Handaec8e6a42010-02-11 09:43:20 +090072 if (domain)
73 atomic_dec(&domain->users);
David Howellsee18d642009-09-02 09:14:21 +010074}
75
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +090076/**
77 * tomoyo_bprm_set_creds - Target for security_bprm_set_creds().
78 *
79 * @bprm: Pointer to "struct linux_binprm".
80 *
81 * Returns 0 on success, negative value otherwise.
82 */
Kentaro Takedaf7433242009-02-05 17:18:16 +090083static int tomoyo_bprm_set_creds(struct linux_binprm *bprm)
84{
Casey Schaufler43fc4602018-09-21 17:18:07 -070085 struct tomoyo_domain_info **blob;
86 struct tomoyo_domain_info *domain;
87
Kentaro Takedaf7433242009-02-05 17:18:16 +090088 /*
89 * Do only if this function is called for the first time of an execve
90 * operation.
91 */
Kees Cookddb4a142017-07-18 15:25:23 -070092 if (bprm->called_set_creds)
Kentaro Takedaf7433242009-02-05 17:18:16 +090093 return 0;
Tetsuo Handa7986cf22011-06-29 13:07:52 +090094#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
Kentaro Takedaf7433242009-02-05 17:18:16 +090095 /*
96 * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
97 * for the first time.
98 */
99 if (!tomoyo_policy_loaded)
100 tomoyo_load_policy(bprm->filename);
Tetsuo Handa7986cf22011-06-29 13:07:52 +0900101#endif
Kentaro Takedaf7433242009-02-05 17:18:16 +0900102 /*
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900103 * Release reference to "struct tomoyo_domain_info" stored inside
104 * "bprm->cred->security". New reference to "struct tomoyo_domain_info"
105 * stored inside "bprm->cred->security" will be acquired later inside
106 * tomoyo_find_next_domain().
107 */
Casey Schaufler43fc4602018-09-21 17:18:07 -0700108 blob = tomoyo_cred(bprm->cred);
109 domain = *blob;
110 atomic_dec(&domain->users);
Tetsuo Handaec8e6a42010-02-11 09:43:20 +0900111 /*
Kentaro Takedaf7433242009-02-05 17:18:16 +0900112 * Tell tomoyo_bprm_check_security() is called for the first time of an
113 * execve operation.
114 */
Casey Schaufler43fc4602018-09-21 17:18:07 -0700115 *blob = NULL;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900116 return 0;
117}
118
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900119/**
120 * tomoyo_bprm_check_security - Target for security_bprm_check().
121 *
122 * @bprm: Pointer to "struct linux_binprm".
123 *
124 * Returns 0 on success, negative value otherwise.
125 */
Kentaro Takedaf7433242009-02-05 17:18:16 +0900126static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
127{
Casey Schaufler43fc4602018-09-21 17:18:07 -0700128 struct tomoyo_domain_info **blob;
129 struct tomoyo_domain_info *domain;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900130
Casey Schaufler43fc4602018-09-21 17:18:07 -0700131 blob = tomoyo_cred(bprm->cred);
132 domain = *blob;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900133 /*
134 * Execute permission is checked against pathname passed to do_execve()
135 * using current domain.
136 */
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900137 if (!domain) {
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900138 const int idx = tomoyo_read_lock();
139 const int err = tomoyo_find_next_domain(bprm);
140 tomoyo_read_unlock(idx);
141 return err;
142 }
Kentaro Takedaf7433242009-02-05 17:18:16 +0900143 /*
144 * Read permission is checked against interpreters using next domain.
Kentaro Takedaf7433242009-02-05 17:18:16 +0900145 */
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900146 return tomoyo_check_open_permission(domain, &bprm->file->f_path,
147 O_RDONLY);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900148}
149
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900150/**
151 * tomoyo_inode_getattr - Target for security_inode_getattr().
152 *
153 * @mnt: Pointer to "struct vfsmount".
154 * @dentry: Pointer to "struct dentry".
155 *
156 * Returns 0 on success, negative value otherwise.
157 */
Al Viro3f7036a2015-03-08 19:28:30 -0400158static int tomoyo_inode_getattr(const struct path *path)
Tetsuo Handa7c759642011-06-26 23:15:31 +0900159{
Al Viro3f7036a2015-03-08 19:28:30 -0400160 return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL);
Tetsuo Handa7c759642011-06-26 23:15:31 +0900161}
162
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900163/**
164 * tomoyo_path_truncate - Target for security_path_truncate().
165 *
166 * @path: Pointer to "struct path".
167 *
168 * Returns 0 on success, negative value otherwise.
169 */
Al Viro81f4c502016-03-25 14:22:01 -0400170static int tomoyo_path_truncate(const struct path *path)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900171{
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900172 return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900173}
174
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900175/**
176 * tomoyo_path_unlink - Target for security_path_unlink().
177 *
178 * @parent: Pointer to "struct path".
179 * @dentry: Pointer to "struct dentry".
180 *
181 * Returns 0 on success, negative value otherwise.
182 */
Al Viro989f74e2016-03-25 15:13:39 -0400183static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry)
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 Handa97fb35e2011-07-08 13:25:53 +0900186 return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900187}
188
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900189/**
190 * tomoyo_path_mkdir - Target for security_path_mkdir().
191 *
192 * @parent: Pointer to "struct path".
193 * @dentry: Pointer to "struct dentry".
194 * @mode: DAC permission mode.
195 *
196 * Returns 0 on success, negative value otherwise.
197 */
Al Virod3607752016-03-25 15:21:09 -0400198static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry,
Al Viro4572bef2011-11-21 14:56:21 -0500199 umode_t mode)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900200{
Kees Cook82917982017-03-29 16:52:58 -0700201 struct path path = { .mnt = parent->mnt, .dentry = dentry };
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900202 return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path,
203 mode & S_IALLUGO);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900204}
205
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900206/**
207 * tomoyo_path_rmdir - Target for security_path_rmdir().
208 *
209 * @parent: Pointer to "struct path".
210 * @dentry: Pointer to "struct dentry".
211 *
212 * Returns 0 on success, negative value otherwise.
213 */
Al Viro989f74e2016-03-25 15:13:39 -0400214static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900215{
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_RMDIR, &path, NULL);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900218}
219
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900220/**
221 * tomoyo_path_symlink - Target for security_path_symlink().
222 *
223 * @parent: Pointer to "struct path".
224 * @dentry: Pointer to "struct dentry".
225 * @old_name: Symlink's content.
226 *
227 * Returns 0 on success, negative value otherwise.
228 */
Al Virod3607752016-03-25 15:21:09 -0400229static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900230 const char *old_name)
231{
Kees Cook82917982017-03-29 16:52:58 -0700232 struct path path = { .mnt = parent->mnt, .dentry = dentry };
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900233 return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900234}
235
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900236/**
237 * tomoyo_path_mknod - Target for security_path_mknod().
238 *
239 * @parent: Pointer to "struct path".
240 * @dentry: Pointer to "struct dentry".
241 * @mode: DAC permission mode.
242 * @dev: Device attributes.
243 *
244 * Returns 0 on success, negative value otherwise.
245 */
Al Virod3607752016-03-25 15:21:09 -0400246static int tomoyo_path_mknod(const struct path *parent, struct dentry *dentry,
Al Viro04fc66e2011-11-21 14:58:38 -0500247 umode_t mode, unsigned int dev)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900248{
Kees Cook82917982017-03-29 16:52:58 -0700249 struct path path = { .mnt = parent->mnt, .dentry = dentry };
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900250 int type = TOMOYO_TYPE_CREATE;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900251 const unsigned int perm = mode & S_IALLUGO;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900252
253 switch (mode & S_IFMT) {
254 case S_IFCHR:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900255 type = TOMOYO_TYPE_MKCHAR;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900256 break;
257 case S_IFBLK:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900258 type = TOMOYO_TYPE_MKBLOCK;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900259 break;
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900260 default:
261 goto no_dev;
262 }
Tetsuo Handa75093152010-06-16 16:23:55 +0900263 return tomoyo_mkdev_perm(type, &path, perm, dev);
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900264 no_dev:
265 switch (mode & S_IFMT) {
Kentaro Takedaf7433242009-02-05 17:18:16 +0900266 case S_IFIFO:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900267 type = TOMOYO_TYPE_MKFIFO;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900268 break;
269 case S_IFSOCK:
Tetsuo Handa7ef61232010-02-16 08:03:30 +0900270 type = TOMOYO_TYPE_MKSOCK;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900271 break;
272 }
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900273 return tomoyo_path_number_perm(type, &path, perm);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900274}
275
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900276/**
277 * tomoyo_path_link - Target for security_path_link().
278 *
279 * @old_dentry: Pointer to "struct dentry".
280 * @new_dir: Pointer to "struct path".
281 * @new_dentry: Pointer to "struct dentry".
282 *
283 * Returns 0 on success, negative value otherwise.
284 */
Al Viro3ccee462016-03-25 15:27:45 -0400285static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_dir,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900286 struct dentry *new_dentry)
287{
Kees Cook82917982017-03-29 16:52:58 -0700288 struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry };
289 struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900290 return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900291}
292
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900293/**
294 * tomoyo_path_rename - Target for security_path_rename().
295 *
296 * @old_parent: Pointer to "struct path".
297 * @old_dentry: Pointer to "struct dentry".
298 * @new_parent: Pointer to "struct path".
299 * @new_dentry: Pointer to "struct dentry".
300 *
301 * Returns 0 on success, negative value otherwise.
302 */
Al Viro3ccee462016-03-25 15:27:45 -0400303static int tomoyo_path_rename(const struct path *old_parent,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900304 struct dentry *old_dentry,
Al Viro3ccee462016-03-25 15:27:45 -0400305 const struct path *new_parent,
Kentaro Takedaf7433242009-02-05 17:18:16 +0900306 struct dentry *new_dentry)
307{
Kees Cook82917982017-03-29 16:52:58 -0700308 struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry };
309 struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry };
Tetsuo Handa97d69312010-02-16 09:46:15 +0900310 return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2);
Kentaro Takedaf7433242009-02-05 17:18:16 +0900311}
312
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900313/**
314 * tomoyo_file_fcntl - Target for security_file_fcntl().
315 *
316 * @file: Pointer to "struct file".
317 * @cmd: Command for fcntl().
318 * @arg: Argument for @cmd.
319 *
320 * Returns 0 on success, negative value otherwise.
321 */
Kentaro Takedaf7433242009-02-05 17:18:16 +0900322static int tomoyo_file_fcntl(struct file *file, unsigned int cmd,
323 unsigned long arg)
324{
Tetsuo Handa7c759642011-06-26 23:15:31 +0900325 if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
326 return 0;
327 return tomoyo_check_open_permission(tomoyo_domain(), &file->f_path,
328 O_WRONLY | (arg & O_APPEND));
Kentaro Takedaf7433242009-02-05 17:18:16 +0900329}
330
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900331/**
Eric Paris83d49852012-04-04 13:45:40 -0400332 * tomoyo_file_open - Target for security_file_open().
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900333 *
334 * @f: Pointer to "struct file".
335 * @cred: Pointer to "struct cred".
336 *
337 * Returns 0 on success, negative value otherwise.
338 */
Al Viro94817692018-07-10 14:13:18 -0400339static int tomoyo_file_open(struct file *f)
Kentaro Takedaf7433242009-02-05 17:18:16 +0900340{
341 int flags = f->f_flags;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900342 /* Don't check read permission here if called from do_execve(). */
343 if (current->in_execve)
344 return 0;
345 return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, flags);
346}
347
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900348/**
349 * tomoyo_file_ioctl - Target for security_file_ioctl().
350 *
351 * @file: Pointer to "struct file".
352 * @cmd: Command for ioctl().
353 * @arg: Argument for @cmd.
354 *
355 * Returns 0 on success, negative value otherwise.
356 */
Tetsuo Handa937bf612009-12-02 21:09:48 +0900357static int tomoyo_file_ioctl(struct file *file, unsigned int cmd,
358 unsigned long arg)
359{
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900360 return tomoyo_path_number_perm(TOMOYO_TYPE_IOCTL, &file->f_path, cmd);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900361}
362
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900363/**
364 * tomoyo_path_chmod - Target for security_path_chmod().
365 *
Al Virocdcf1162011-12-08 10:51:53 -0500366 * @path: Pointer to "struct path".
367 * @mode: DAC permission mode.
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900368 *
369 * Returns 0 on success, negative value otherwise.
370 */
Al Virobe01f9f2016-03-25 14:56:23 -0400371static int tomoyo_path_chmod(const struct path *path, umode_t mode)
Tetsuo Handa937bf612009-12-02 21:09:48 +0900372{
Al Virocdcf1162011-12-08 10:51:53 -0500373 return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path,
Tetsuo Handaa1f9bb62010-05-17 10:09:15 +0900374 mode & S_IALLUGO);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900375}
376
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900377/**
378 * tomoyo_path_chown - Target for security_path_chown().
379 *
380 * @path: Pointer to "struct path".
381 * @uid: Owner ID.
382 * @gid: Group ID.
383 *
384 * Returns 0 on success, negative value otherwise.
385 */
Al Viro7fd25da2016-03-25 14:44:41 -0400386static int tomoyo_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
Tetsuo Handa937bf612009-12-02 21:09:48 +0900387{
388 int error = 0;
Eric W. Biedermand2b31ca2012-06-01 16:14:19 -0600389 if (uid_valid(uid))
390 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path,
391 from_kuid(&init_user_ns, uid));
392 if (!error && gid_valid(gid))
393 error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path,
394 from_kgid(&init_user_ns, gid));
Tetsuo Handa937bf612009-12-02 21:09:48 +0900395 return error;
396}
397
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900398/**
399 * tomoyo_path_chroot - Target for security_path_chroot().
400 *
401 * @path: Pointer to "struct path".
402 *
403 * Returns 0 on success, negative value otherwise.
404 */
Al Viro77b286c2016-03-25 15:28:43 -0400405static int tomoyo_path_chroot(const struct path *path)
Tetsuo Handa937bf612009-12-02 21:09:48 +0900406{
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900407 return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900408}
409
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900410/**
411 * tomoyo_sb_mount - Target for security_sb_mount().
412 *
413 * @dev_name: Name of device file. Maybe NULL.
414 * @path: Pointer to "struct path".
415 * @type: Name of filesystem type. Maybe NULL.
416 * @flags: Mount options.
417 * @data: Optional data. Maybe NULL.
418 *
419 * Returns 0 on success, negative value otherwise.
420 */
Al Viro8a04c432016-03-25 14:52:53 -0400421static int tomoyo_sb_mount(const char *dev_name, const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -0400422 const char *type, unsigned long flags, void *data)
Tetsuo Handa937bf612009-12-02 21:09:48 +0900423{
Tetsuo Handa2106ccd2010-05-17 10:10:31 +0900424 return tomoyo_mount_permission(dev_name, path, type, flags, data);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900425}
426
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900427/**
428 * tomoyo_sb_umount - Target for security_sb_umount().
429 *
430 * @mnt: Pointer to "struct vfsmount".
431 * @flags: Unmount options.
432 *
433 * Returns 0 on success, negative value otherwise.
434 */
Tetsuo Handa937bf612009-12-02 21:09:48 +0900435static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
436{
Kees Cook82917982017-03-29 16:52:58 -0700437 struct path path = { .mnt = mnt, .dentry = mnt->mnt_root };
Tetsuo Handa97fb35e2011-07-08 13:25:53 +0900438 return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900439}
440
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900441/**
442 * tomoyo_sb_pivotroot - Target for security_sb_pivotroot().
443 *
444 * @old_path: Pointer to "struct path".
445 * @new_path: Pointer to "struct path".
446 *
447 * Returns 0 on success, negative value otherwise.
448 */
Al Viro3b73b682016-03-25 15:31:19 -0400449static int tomoyo_sb_pivotroot(const struct path *old_path, const struct path *new_path)
Tetsuo Handa937bf612009-12-02 21:09:48 +0900450{
Tetsuo Handa97d69312010-02-16 09:46:15 +0900451 return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path);
Tetsuo Handa937bf612009-12-02 21:09:48 +0900452}
453
Tetsuo Handa059d84d2011-09-10 15:23:54 +0900454/**
455 * tomoyo_socket_listen - Check permission for listen().
456 *
457 * @sock: Pointer to "struct socket".
458 * @backlog: Backlog parameter.
459 *
460 * Returns 0 on success, negative value otherwise.
461 */
462static int tomoyo_socket_listen(struct socket *sock, int backlog)
463{
464 return tomoyo_socket_listen_permission(sock);
465}
466
467/**
468 * tomoyo_socket_connect - Check permission for connect().
469 *
470 * @sock: Pointer to "struct socket".
471 * @addr: Pointer to "struct sockaddr".
472 * @addr_len: Size of @addr.
473 *
474 * Returns 0 on success, negative value otherwise.
475 */
476static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr,
477 int addr_len)
478{
479 return tomoyo_socket_connect_permission(sock, addr, addr_len);
480}
481
482/**
483 * tomoyo_socket_bind - Check permission for bind().
484 *
485 * @sock: Pointer to "struct socket".
486 * @addr: Pointer to "struct sockaddr".
487 * @addr_len: Size of @addr.
488 *
489 * Returns 0 on success, negative value otherwise.
490 */
491static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr,
492 int addr_len)
493{
494 return tomoyo_socket_bind_permission(sock, addr, addr_len);
495}
496
497/**
498 * tomoyo_socket_sendmsg - Check permission for sendmsg().
499 *
500 * @sock: Pointer to "struct socket".
501 * @msg: Pointer to "struct msghdr".
502 * @size: Size of message.
503 *
504 * Returns 0 on success, negative value otherwise.
505 */
506static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
507 int size)
508{
509 return tomoyo_socket_sendmsg_permission(sock, msg, size);
510}
511
Casey Schauflerbbd36622018-11-12 09:30:56 -0800512struct lsm_blob_sizes tomoyo_blob_sizes __lsm_ro_after_init = {
513 .lbs_cred = sizeof(struct tomoyo_domain_info *),
514};
515
Tetsuo Handac3fa1092009-06-08 12:37:39 +0900516/*
517 * tomoyo_security_ops is a "struct security_operations" which is used for
518 * registering TOMOYO.
519 */
James Morrisca97d932017-02-15 00:18:51 +1100520static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -0700521 LSM_HOOK_INIT(cred_alloc_blank, tomoyo_cred_alloc_blank),
522 LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare),
523 LSM_HOOK_INIT(cred_transfer, tomoyo_cred_transfer),
524 LSM_HOOK_INIT(cred_free, tomoyo_cred_free),
525 LSM_HOOK_INIT(bprm_set_creds, tomoyo_bprm_set_creds),
526 LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
527 LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
528 LSM_HOOK_INIT(file_open, tomoyo_file_open),
529 LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate),
530 LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink),
531 LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir),
532 LSM_HOOK_INIT(path_rmdir, tomoyo_path_rmdir),
533 LSM_HOOK_INIT(path_symlink, tomoyo_path_symlink),
534 LSM_HOOK_INIT(path_mknod, tomoyo_path_mknod),
535 LSM_HOOK_INIT(path_link, tomoyo_path_link),
536 LSM_HOOK_INIT(path_rename, tomoyo_path_rename),
537 LSM_HOOK_INIT(inode_getattr, tomoyo_inode_getattr),
538 LSM_HOOK_INIT(file_ioctl, tomoyo_file_ioctl),
539 LSM_HOOK_INIT(path_chmod, tomoyo_path_chmod),
540 LSM_HOOK_INIT(path_chown, tomoyo_path_chown),
541 LSM_HOOK_INIT(path_chroot, tomoyo_path_chroot),
542 LSM_HOOK_INIT(sb_mount, tomoyo_sb_mount),
543 LSM_HOOK_INIT(sb_umount, tomoyo_sb_umount),
544 LSM_HOOK_INIT(sb_pivotroot, tomoyo_sb_pivotroot),
545 LSM_HOOK_INIT(socket_bind, tomoyo_socket_bind),
546 LSM_HOOK_INIT(socket_connect, tomoyo_socket_connect),
547 LSM_HOOK_INIT(socket_listen, tomoyo_socket_listen),
548 LSM_HOOK_INIT(socket_sendmsg, tomoyo_socket_sendmsg),
Kentaro Takedaf7433242009-02-05 17:18:16 +0900549};
550
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900551/* Lock for GC. */
Lai Jiangshan505f14f2013-03-16 00:50:53 +0800552DEFINE_SRCU(tomoyo_ss);
Tetsuo Handafdb8ebb2009-12-08 09:34:43 +0900553
Casey Schaufler43fc4602018-09-21 17:18:07 -0700554int tomoyo_enabled __lsm_ro_after_init = 1;
555
Tetsuo Handa0f2a55d2011-07-14 14:46:51 +0900556/**
557 * tomoyo_init - Register TOMOYO Linux as a LSM module.
558 *
559 * Returns 0.
560 */
Kentaro Takedaf7433242009-02-05 17:18:16 +0900561static int __init tomoyo_init(void)
562{
563 struct cred *cred = (struct cred *) current_cred();
Casey Schaufler43fc4602018-09-21 17:18:07 -0700564 struct tomoyo_domain_info **blob;
Kentaro Takedaf7433242009-02-05 17:18:16 +0900565
Kentaro Takedaf7433242009-02-05 17:18:16 +0900566 /* register ourselves with the security framework */
Casey Schauflerd69dece52017-01-18 17:09:05 -0800567 security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), "tomoyo");
Kentaro Takedaf7433242009-02-05 17:18:16 +0900568 printk(KERN_INFO "TOMOYO Linux initialized\n");
Casey Schauflerbbd36622018-11-12 09:30:56 -0800569 lsm_early_cred(cred);
Casey Schaufler43fc4602018-09-21 17:18:07 -0700570 blob = tomoyo_cred(cred);
571 *blob = &tomoyo_kernel_domain;
Tetsuo Handac3ef1502010-05-17 10:12:46 +0900572 tomoyo_mm_init();
Casey Schaufler43fc4602018-09-21 17:18:07 -0700573
Kentaro Takedaf7433242009-02-05 17:18:16 +0900574 return 0;
575}
576
Kees Cook3d6e5f62018-10-10 17:18:23 -0700577DEFINE_LSM(tomoyo) = {
Kees Cook07aed2f2018-10-10 17:18:24 -0700578 .name = "tomoyo",
Casey Schaufler43fc4602018-09-21 17:18:07 -0700579 .enabled = &tomoyo_enabled,
Kees Cook14bd99c2018-09-19 19:57:06 -0700580 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -0800581 .blobs = &tomoyo_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -0700582 .init = tomoyo_init,
583};