Commit | Line | Data |
---|---|---|
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> |
f3b8788c | 9 | #include <uapi/linux/lsm.h> |
f7433243 | 10 | #include "common.h" |
f7433243 | 11 | |
0f2a55d5 | 12 | /** |
8c6cb983 | 13 | * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread. |
0f2a55d5 | 14 | * |
8c6cb983 | 15 | * Returns pointer to "struct tomoyo_domain_info" for current thread. |
0f2a55d5 | 16 | */ |
8c6cb983 | 17 | struct tomoyo_domain_info *tomoyo_domain(void) |
ee18d64c | 18 | { |
8c6cb983 | 19 | struct tomoyo_task *s = tomoyo_task(current); |
43fc4609 | 20 | |
8c6cb983 TH |
21 | if (s->old_domain_info && !current->in_execve) { |
22 | atomic_dec(&s->old_domain_info->users); | |
23 | s->old_domain_info = NULL; | |
24 | } | |
25 | return s->domain_info; | |
ee18d64c DH |
26 | } |
27 | ||
0f2a55d5 TH |
28 | /** |
29 | * tomoyo_cred_prepare - Target for security_prepare_creds(). | |
30 | * | |
31 | * @new: Pointer to "struct cred". | |
32 | * @old: Pointer to "struct cred". | |
33 | * @gfp: Memory allocation flags. | |
34 | * | |
35 | * Returns 0. | |
36 | */ | |
f7433243 KT |
37 | static int tomoyo_cred_prepare(struct cred *new, const struct cred *old, |
38 | gfp_t gfp) | |
39 | { | |
8c6cb983 TH |
40 | /* Restore old_domain_info saved by previous execve() request. */ |
41 | struct tomoyo_task *s = tomoyo_task(current); | |
43fc4609 | 42 | |
8c6cb983 TH |
43 | if (s->old_domain_info && !current->in_execve) { |
44 | atomic_dec(&s->domain_info->users); | |
45 | s->domain_info = s->old_domain_info; | |
46 | s->old_domain_info = NULL; | |
47 | } | |
f7433243 KT |
48 | return 0; |
49 | } | |
50 | ||
0f2a55d5 | 51 | /** |
8c6cb983 | 52 | * tomoyo_bprm_committed_creds - Target for security_bprm_committed_creds(). |
0f2a55d5 | 53 | * |
8c6cb983 | 54 | * @bprm: Pointer to "struct linux_binprm". |
0f2a55d5 | 55 | */ |
a721f7b8 | 56 | static void tomoyo_bprm_committed_creds(const struct linux_binprm *bprm) |
ec8e6a4e | 57 | { |
8c6cb983 TH |
58 | /* Clear old_domain_info saved by execve() request. */ |
59 | struct tomoyo_task *s = tomoyo_task(current); | |
43fc4609 | 60 | |
8c6cb983 TH |
61 | atomic_dec(&s->old_domain_info->users); |
62 | s->old_domain_info = NULL; | |
ee18d64c DH |
63 | } |
64 | ||
8c6cb983 | 65 | #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER |
0f2a55d5 | 66 | /** |
98eaa63e | 67 | * tomoyo_bprm_creds_for_exec - Target for security_bprm_creds_for_exec(). |
0f2a55d5 TH |
68 | * |
69 | * @bprm: Pointer to "struct linux_binprm". | |
70 | * | |
8c6cb983 | 71 | * Returns 0. |
0f2a55d5 | 72 | */ |
b8bff599 | 73 | static int tomoyo_bprm_creds_for_exec(struct linux_binprm *bprm) |
f7433243 | 74 | { |
f7433243 KT |
75 | /* |
76 | * Load policy if /sbin/tomoyo-init exists and /sbin/init is requested | |
77 | * for the first time. | |
78 | */ | |
79 | if (!tomoyo_policy_loaded) | |
80 | tomoyo_load_policy(bprm->filename); | |
f7433243 KT |
81 | return 0; |
82 | } | |
8c6cb983 | 83 | #endif |
f7433243 | 84 | |
0f2a55d5 TH |
85 | /** |
86 | * tomoyo_bprm_check_security - Target for security_bprm_check(). | |
87 | * | |
88 | * @bprm: Pointer to "struct linux_binprm". | |
89 | * | |
90 | * Returns 0 on success, negative value otherwise. | |
91 | */ | |
f7433243 KT |
92 | static int tomoyo_bprm_check_security(struct linux_binprm *bprm) |
93 | { | |
8c6cb983 | 94 | struct tomoyo_task *s = tomoyo_task(current); |
f7433243 KT |
95 | |
96 | /* | |
be619f7f | 97 | * Execute permission is checked against pathname passed to execve() |
f7433243 KT |
98 | * using current domain. |
99 | */ | |
8c6cb983 | 100 | if (!s->old_domain_info) { |
fdb8ebb7 TH |
101 | const int idx = tomoyo_read_lock(); |
102 | const int err = tomoyo_find_next_domain(bprm); | |
cdcf6723 | 103 | |
fdb8ebb7 TH |
104 | tomoyo_read_unlock(idx); |
105 | return err; | |
106 | } | |
f7433243 KT |
107 | /* |
108 | * Read permission is checked against interpreters using next domain. | |
f7433243 | 109 | */ |
8c6cb983 TH |
110 | return tomoyo_check_open_permission(s->domain_info, |
111 | &bprm->file->f_path, O_RDONLY); | |
f7433243 KT |
112 | } |
113 | ||
0f2a55d5 TH |
114 | /** |
115 | * tomoyo_inode_getattr - Target for security_inode_getattr(). | |
116 | * | |
98eaa63e | 117 | * @path: Pointer to "struct path". |
0f2a55d5 TH |
118 | * |
119 | * Returns 0 on success, negative value otherwise. | |
120 | */ | |
3f7036a0 | 121 | static int tomoyo_inode_getattr(const struct path *path) |
7c75964f | 122 | { |
3f7036a0 | 123 | return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, path, NULL); |
7c75964f TH |
124 | } |
125 | ||
0f2a55d5 TH |
126 | /** |
127 | * tomoyo_path_truncate - Target for security_path_truncate(). | |
128 | * | |
129 | * @path: Pointer to "struct path". | |
130 | * | |
131 | * Returns 0 on success, negative value otherwise. | |
132 | */ | |
81f4c506 | 133 | static int tomoyo_path_truncate(const struct path *path) |
f7433243 | 134 | { |
97fb35e4 | 135 | return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL); |
f7433243 KT |
136 | } |
137 | ||
3350607d GN |
138 | /** |
139 | * tomoyo_file_truncate - Target for security_file_truncate(). | |
140 | * | |
141 | * @file: Pointer to "struct file". | |
142 | * | |
143 | * Returns 0 on success, negative value otherwise. | |
144 | */ | |
145 | static int tomoyo_file_truncate(struct file *file) | |
146 | { | |
147 | return tomoyo_path_truncate(&file->f_path); | |
148 | } | |
149 | ||
0f2a55d5 TH |
150 | /** |
151 | * tomoyo_path_unlink - Target for security_path_unlink(). | |
152 | * | |
153 | * @parent: Pointer to "struct path". | |
154 | * @dentry: Pointer to "struct dentry". | |
155 | * | |
156 | * Returns 0 on success, negative value otherwise. | |
157 | */ | |
989f74e0 | 158 | static int tomoyo_path_unlink(const struct path *parent, struct dentry *dentry) |
f7433243 | 159 | { |
8291798d | 160 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
cdcf6723 | 161 | |
97fb35e4 | 162 | return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL); |
f7433243 KT |
163 | } |
164 | ||
0f2a55d5 TH |
165 | /** |
166 | * tomoyo_path_mkdir - Target for security_path_mkdir(). | |
167 | * | |
168 | * @parent: Pointer to "struct path". | |
169 | * @dentry: Pointer to "struct dentry". | |
170 | * @mode: DAC permission mode. | |
171 | * | |
172 | * Returns 0 on success, negative value otherwise. | |
173 | */ | |
d3607752 | 174 | static int tomoyo_path_mkdir(const struct path *parent, struct dentry *dentry, |
4572befe | 175 | umode_t mode) |
f7433243 | 176 | { |
8291798d | 177 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
cdcf6723 | 178 | |
a1f9bb6a TH |
179 | return tomoyo_path_number_perm(TOMOYO_TYPE_MKDIR, &path, |
180 | mode & S_IALLUGO); | |
f7433243 KT |
181 | } |
182 | ||
0f2a55d5 TH |
183 | /** |
184 | * tomoyo_path_rmdir - Target for security_path_rmdir(). | |
185 | * | |
186 | * @parent: Pointer to "struct path". | |
187 | * @dentry: Pointer to "struct dentry". | |
188 | * | |
189 | * Returns 0 on success, negative value otherwise. | |
190 | */ | |
989f74e0 | 191 | static int tomoyo_path_rmdir(const struct path *parent, struct dentry *dentry) |
f7433243 | 192 | { |
8291798d | 193 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
cdcf6723 | 194 | |
97fb35e4 | 195 | return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL); |
f7433243 KT |
196 | } |
197 | ||
0f2a55d5 TH |
198 | /** |
199 | * tomoyo_path_symlink - Target for security_path_symlink(). | |
200 | * | |
201 | * @parent: Pointer to "struct path". | |
202 | * @dentry: Pointer to "struct dentry". | |
203 | * @old_name: Symlink's content. | |
204 | * | |
205 | * Returns 0 on success, negative value otherwise. | |
206 | */ | |
d3607752 | 207 | static int tomoyo_path_symlink(const struct path *parent, struct dentry *dentry, |
f7433243 KT |
208 | const char *old_name) |
209 | { | |
8291798d | 210 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
cdcf6723 | 211 | |
97fb35e4 | 212 | return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name); |
f7433243 KT |
213 | } |
214 | ||
0f2a55d5 TH |
215 | /** |
216 | * tomoyo_path_mknod - Target for security_path_mknod(). | |
217 | * | |
218 | * @parent: Pointer to "struct path". | |
219 | * @dentry: Pointer to "struct dentry". | |
220 | * @mode: DAC permission mode. | |
221 | * @dev: Device attributes. | |
222 | * | |
223 | * Returns 0 on success, negative value otherwise. | |
224 | */ | |
d3607752 | 225 | static int tomoyo_path_mknod(const struct path *parent, struct dentry *dentry, |
04fc66e7 | 226 | umode_t mode, unsigned int dev) |
f7433243 | 227 | { |
8291798d | 228 | struct path path = { .mnt = parent->mnt, .dentry = dentry }; |
7ef61233 | 229 | int type = TOMOYO_TYPE_CREATE; |
a1f9bb6a | 230 | const unsigned int perm = mode & S_IALLUGO; |
f7433243 KT |
231 | |
232 | switch (mode & S_IFMT) { | |
233 | case S_IFCHR: | |
7ef61233 | 234 | type = TOMOYO_TYPE_MKCHAR; |
f7433243 KT |
235 | break; |
236 | case S_IFBLK: | |
7ef61233 | 237 | type = TOMOYO_TYPE_MKBLOCK; |
f7433243 | 238 | break; |
a1f9bb6a TH |
239 | default: |
240 | goto no_dev; | |
241 | } | |
75093152 | 242 | return tomoyo_mkdev_perm(type, &path, perm, dev); |
a1f9bb6a TH |
243 | no_dev: |
244 | switch (mode & S_IFMT) { | |
f7433243 | 245 | case S_IFIFO: |
7ef61233 | 246 | type = TOMOYO_TYPE_MKFIFO; |
f7433243 KT |
247 | break; |
248 | case S_IFSOCK: | |
7ef61233 | 249 | type = TOMOYO_TYPE_MKSOCK; |
f7433243 KT |
250 | break; |
251 | } | |
a1f9bb6a | 252 | return tomoyo_path_number_perm(type, &path, perm); |
f7433243 KT |
253 | } |
254 | ||
0f2a55d5 TH |
255 | /** |
256 | * tomoyo_path_link - Target for security_path_link(). | |
257 | * | |
258 | * @old_dentry: Pointer to "struct dentry". | |
259 | * @new_dir: Pointer to "struct path". | |
260 | * @new_dentry: Pointer to "struct dentry". | |
261 | * | |
262 | * Returns 0 on success, negative value otherwise. | |
263 | */ | |
3ccee46a | 264 | static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_dir, |
f7433243 KT |
265 | struct dentry *new_dentry) |
266 | { | |
8291798d KC |
267 | struct path path1 = { .mnt = new_dir->mnt, .dentry = old_dentry }; |
268 | struct path path2 = { .mnt = new_dir->mnt, .dentry = new_dentry }; | |
cdcf6723 | 269 | |
97d6931e | 270 | return tomoyo_path2_perm(TOMOYO_TYPE_LINK, &path1, &path2); |
f7433243 KT |
271 | } |
272 | ||
0f2a55d5 TH |
273 | /** |
274 | * tomoyo_path_rename - Target for security_path_rename(). | |
275 | * | |
276 | * @old_parent: Pointer to "struct path". | |
277 | * @old_dentry: Pointer to "struct dentry". | |
278 | * @new_parent: Pointer to "struct path". | |
279 | * @new_dentry: Pointer to "struct dentry". | |
100f59d9 | 280 | * @flags: Rename options. |
0f2a55d5 TH |
281 | * |
282 | * Returns 0 on success, negative value otherwise. | |
283 | */ | |
3ccee46a | 284 | static int tomoyo_path_rename(const struct path *old_parent, |
f7433243 | 285 | struct dentry *old_dentry, |
3ccee46a | 286 | const struct path *new_parent, |
100f59d9 MS |
287 | struct dentry *new_dentry, |
288 | const unsigned int flags) | |
f7433243 | 289 | { |
8291798d KC |
290 | struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry }; |
291 | struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry }; | |
cdcf6723 | 292 | |
100f59d9 MS |
293 | if (flags & RENAME_EXCHANGE) { |
294 | const int err = tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path2, | |
295 | &path1); | |
296 | ||
297 | if (err) | |
298 | return err; | |
299 | } | |
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 |
312 | static 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 | 321 | /** |
83d49856 | 322 | * tomoyo_file_open - Target for security_file_open(). |
0f2a55d5 | 323 | * |
98eaa63e | 324 | * @f: Pointer to "struct file". |
0f2a55d5 TH |
325 | * |
326 | * Returns 0 on success, negative value otherwise. | |
327 | */ | |
94817692 | 328 | static int tomoyo_file_open(struct file *f) |
f7433243 | 329 | { |
be619f7f | 330 | /* Don't check read permission here if called from execve(). */ |
4759ff71 KC |
331 | /* Illogically, FMODE_EXEC is in f_flags, not f_mode. */ |
332 | if (f->f_flags & __FMODE_EXEC) | |
f7433243 | 333 | return 0; |
cdcf6723 TH |
334 | return tomoyo_check_open_permission(tomoyo_domain(), &f->f_path, |
335 | f->f_flags); | |
f7433243 KT |
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 |
347 | static 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 | * | |
cdcf116d AV |
356 | * @path: Pointer to "struct path". |
357 | * @mode: DAC permission mode. | |
0f2a55d5 TH |
358 | * |
359 | * Returns 0 on success, negative value otherwise. | |
360 | */ | |
be01f9f2 | 361 | static int tomoyo_path_chmod(const struct path *path, umode_t mode) |
937bf613 | 362 | { |
cdcf116d | 363 | return tomoyo_path_number_perm(TOMOYO_TYPE_CHMOD, path, |
a1f9bb6a | 364 | mode & S_IALLUGO); |
937bf613 TH |
365 | } |
366 | ||
0f2a55d5 TH |
367 | /** |
368 | * tomoyo_path_chown - Target for security_path_chown(). | |
369 | * | |
370 | * @path: Pointer to "struct path". | |
371 | * @uid: Owner ID. | |
372 | * @gid: Group ID. | |
373 | * | |
374 | * Returns 0 on success, negative value otherwise. | |
375 | */ | |
7fd25dac | 376 | static int tomoyo_path_chown(const struct path *path, kuid_t uid, kgid_t gid) |
937bf613 TH |
377 | { |
378 | int error = 0; | |
cdcf6723 | 379 | |
d2b31ca6 EB |
380 | if (uid_valid(uid)) |
381 | error = tomoyo_path_number_perm(TOMOYO_TYPE_CHOWN, path, | |
382 | from_kuid(&init_user_ns, uid)); | |
383 | if (!error && gid_valid(gid)) | |
384 | error = tomoyo_path_number_perm(TOMOYO_TYPE_CHGRP, path, | |
385 | from_kgid(&init_user_ns, 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 | */ | |
77b286c0 | 396 | static int tomoyo_path_chroot(const struct path *path) |
937bf613 | 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 | */ | |
8a04c43b | 412 | static int tomoyo_sb_mount(const char *dev_name, const struct path *path, |
808d4e3c | 413 | const char *type, unsigned long flags, void *data) |
937bf613 | 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 |
426 | static int tomoyo_sb_umount(struct vfsmount *mnt, int flags) |
427 | { | |
8291798d | 428 | struct path path = { .mnt = mnt, .dentry = mnt->mnt_root }; |
cdcf6723 | 429 | |
97fb35e4 | 430 | return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL); |
937bf613 TH |
431 | } |
432 | ||
0f2a55d5 TH |
433 | /** |
434 | * tomoyo_sb_pivotroot - Target for security_sb_pivotroot(). | |
435 | * | |
436 | * @old_path: Pointer to "struct path". | |
437 | * @new_path: Pointer to "struct path". | |
438 | * | |
439 | * Returns 0 on success, negative value otherwise. | |
440 | */ | |
3b73b68c | 441 | static int tomoyo_sb_pivotroot(const struct path *old_path, const struct path *new_path) |
937bf613 | 442 | { |
97d6931e | 443 | return tomoyo_path2_perm(TOMOYO_TYPE_PIVOT_ROOT, new_path, old_path); |
937bf613 TH |
444 | } |
445 | ||
059d84db TH |
446 | /** |
447 | * tomoyo_socket_listen - Check permission for listen(). | |
448 | * | |
449 | * @sock: Pointer to "struct socket". | |
450 | * @backlog: Backlog parameter. | |
451 | * | |
452 | * Returns 0 on success, negative value otherwise. | |
453 | */ | |
454 | static int tomoyo_socket_listen(struct socket *sock, int backlog) | |
455 | { | |
456 | return tomoyo_socket_listen_permission(sock); | |
457 | } | |
458 | ||
459 | /** | |
460 | * tomoyo_socket_connect - Check permission for connect(). | |
461 | * | |
462 | * @sock: Pointer to "struct socket". | |
463 | * @addr: Pointer to "struct sockaddr". | |
464 | * @addr_len: Size of @addr. | |
465 | * | |
466 | * Returns 0 on success, negative value otherwise. | |
467 | */ | |
468 | static int tomoyo_socket_connect(struct socket *sock, struct sockaddr *addr, | |
469 | int addr_len) | |
470 | { | |
471 | return tomoyo_socket_connect_permission(sock, addr, addr_len); | |
472 | } | |
473 | ||
474 | /** | |
475 | * tomoyo_socket_bind - Check permission for bind(). | |
476 | * | |
477 | * @sock: Pointer to "struct socket". | |
478 | * @addr: Pointer to "struct sockaddr". | |
479 | * @addr_len: Size of @addr. | |
480 | * | |
481 | * Returns 0 on success, negative value otherwise. | |
482 | */ | |
483 | static int tomoyo_socket_bind(struct socket *sock, struct sockaddr *addr, | |
484 | int addr_len) | |
485 | { | |
486 | return tomoyo_socket_bind_permission(sock, addr, addr_len); | |
487 | } | |
488 | ||
489 | /** | |
490 | * tomoyo_socket_sendmsg - Check permission for sendmsg(). | |
491 | * | |
492 | * @sock: Pointer to "struct socket". | |
493 | * @msg: Pointer to "struct msghdr". | |
494 | * @size: Size of message. | |
495 | * | |
496 | * Returns 0 on success, negative value otherwise. | |
497 | */ | |
498 | static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
499 | int size) | |
500 | { | |
501 | return tomoyo_socket_sendmsg_permission(sock, msg, size); | |
502 | } | |
503 | ||
f22f9aaf | 504 | struct lsm_blob_sizes tomoyo_blob_sizes __ro_after_init = { |
8c6cb983 | 505 | .lbs_task = sizeof(struct tomoyo_task), |
bbd3662a CS |
506 | }; |
507 | ||
8c6cb983 TH |
508 | /** |
509 | * tomoyo_task_alloc - Target for security_task_alloc(). | |
510 | * | |
98eaa63e C |
511 | * @task: Pointer to "struct task_struct". |
512 | * @clone_flags: clone() flags. | |
8c6cb983 TH |
513 | * |
514 | * Returns 0. | |
515 | */ | |
516 | static int tomoyo_task_alloc(struct task_struct *task, | |
517 | unsigned long clone_flags) | |
518 | { | |
519 | struct tomoyo_task *old = tomoyo_task(current); | |
520 | struct tomoyo_task *new = tomoyo_task(task); | |
521 | ||
522 | new->domain_info = old->domain_info; | |
523 | atomic_inc(&new->domain_info->users); | |
524 | new->old_domain_info = NULL; | |
525 | return 0; | |
526 | } | |
527 | ||
528 | /** | |
529 | * tomoyo_task_free - Target for security_task_free(). | |
530 | * | |
531 | * @task: Pointer to "struct task_struct". | |
532 | */ | |
533 | static void tomoyo_task_free(struct task_struct *task) | |
534 | { | |
535 | struct tomoyo_task *s = tomoyo_task(task); | |
536 | ||
537 | if (s->domain_info) { | |
538 | atomic_dec(&s->domain_info->users); | |
539 | s->domain_info = NULL; | |
540 | } | |
541 | if (s->old_domain_info) { | |
542 | atomic_dec(&s->old_domain_info->users); | |
543 | s->old_domain_info = NULL; | |
544 | } | |
545 | } | |
546 | ||
b1a867ee | 547 | static const struct lsm_id tomoyo_lsmid = { |
f3b8788c CS |
548 | .name = "tomoyo", |
549 | .id = LSM_ID_TOMOYO, | |
550 | }; | |
551 | ||
c3fa109a TH |
552 | /* |
553 | * tomoyo_security_ops is a "struct security_operations" which is used for | |
554 | * registering TOMOYO. | |
555 | */ | |
f22f9aaf | 556 | static struct security_hook_list tomoyo_hooks[] __ro_after_init = { |
e20b043a | 557 | LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare), |
8c6cb983 TH |
558 | LSM_HOOK_INIT(bprm_committed_creds, tomoyo_bprm_committed_creds), |
559 | LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc), | |
560 | LSM_HOOK_INIT(task_free, tomoyo_task_free), | |
561 | #ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER | |
b8bff599 | 562 | LSM_HOOK_INIT(bprm_creds_for_exec, tomoyo_bprm_creds_for_exec), |
8c6cb983 | 563 | #endif |
e20b043a CS |
564 | LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security), |
565 | LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl), | |
566 | LSM_HOOK_INIT(file_open, tomoyo_file_open), | |
3350607d | 567 | LSM_HOOK_INIT(file_truncate, tomoyo_file_truncate), |
e20b043a CS |
568 | LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate), |
569 | LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink), | |
570 | LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir), | |
571 | LSM_HOOK_INIT(path_rmdir, tomoyo_path_rmdir), | |
572 | LSM_HOOK_INIT(path_symlink, tomoyo_path_symlink), | |
573 | LSM_HOOK_INIT(path_mknod, tomoyo_path_mknod), | |
574 | LSM_HOOK_INIT(path_link, tomoyo_path_link), | |
575 | LSM_HOOK_INIT(path_rename, tomoyo_path_rename), | |
576 | LSM_HOOK_INIT(inode_getattr, tomoyo_inode_getattr), | |
577 | LSM_HOOK_INIT(file_ioctl, tomoyo_file_ioctl), | |
f1bb47a3 | 578 | LSM_HOOK_INIT(file_ioctl_compat, tomoyo_file_ioctl), |
e20b043a CS |
579 | LSM_HOOK_INIT(path_chmod, tomoyo_path_chmod), |
580 | LSM_HOOK_INIT(path_chown, tomoyo_path_chown), | |
581 | LSM_HOOK_INIT(path_chroot, tomoyo_path_chroot), | |
582 | LSM_HOOK_INIT(sb_mount, tomoyo_sb_mount), | |
583 | LSM_HOOK_INIT(sb_umount, tomoyo_sb_umount), | |
584 | LSM_HOOK_INIT(sb_pivotroot, tomoyo_sb_pivotroot), | |
585 | LSM_HOOK_INIT(socket_bind, tomoyo_socket_bind), | |
586 | LSM_HOOK_INIT(socket_connect, tomoyo_socket_connect), | |
587 | LSM_HOOK_INIT(socket_listen, tomoyo_socket_listen), | |
588 | LSM_HOOK_INIT(socket_sendmsg, tomoyo_socket_sendmsg), | |
f7433243 KT |
589 | }; |
590 | ||
fdb8ebb7 | 591 | /* Lock for GC. */ |
505f14f7 | 592 | DEFINE_SRCU(tomoyo_ss); |
fdb8ebb7 | 593 | |
f22f9aaf | 594 | int tomoyo_enabled __ro_after_init = 1; |
43fc4609 | 595 | |
0f2a55d5 TH |
596 | /** |
597 | * tomoyo_init - Register TOMOYO Linux as a LSM module. | |
598 | * | |
599 | * Returns 0. | |
600 | */ | |
f7433243 KT |
601 | static int __init tomoyo_init(void) |
602 | { | |
8c6cb983 | 603 | struct tomoyo_task *s = tomoyo_task(current); |
f7433243 | 604 | |
f7433243 | 605 | /* register ourselves with the security framework */ |
f3b8788c CS |
606 | security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks), |
607 | &tomoyo_lsmid); | |
cdcf6723 | 608 | pr_info("TOMOYO Linux initialized\n"); |
8c6cb983 TH |
609 | s->domain_info = &tomoyo_kernel_domain; |
610 | atomic_inc(&tomoyo_kernel_domain.users); | |
611 | s->old_domain_info = NULL; | |
c3ef1500 | 612 | tomoyo_mm_init(); |
43fc4609 | 613 | |
f7433243 KT |
614 | return 0; |
615 | } | |
616 | ||
3d6e5f6d | 617 | DEFINE_LSM(tomoyo) = { |
07aed2f2 | 618 | .name = "tomoyo", |
43fc4609 | 619 | .enabled = &tomoyo_enabled, |
a5e2fe7e | 620 | .flags = LSM_FLAG_LEGACY_MAJOR, |
bbd3662a | 621 | .blobs = &tomoyo_blob_sizes, |
3d6e5f6d KC |
622 | .init = tomoyo_init, |
623 | }; |