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