^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * AppArmor security module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This file contains AppArmor mediation of files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 1998-2008 Novell/SUSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright 2009-2010 Canonical Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/fdtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "include/apparmor.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "include/audit.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "include/cred.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "include/file.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "include/match.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "include/net.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "include/path.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "include/policy.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "include/label.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static u32 map_mask_to_chr_mask(u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u32 m = mask & PERMS_CHRS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (mask & AA_MAY_GETATTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) m |= MAY_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (mask & (AA_MAY_SETATTR | AA_MAY_CHMOD | AA_MAY_CHOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) m |= MAY_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * file_audit_cb - call back for file specific audit fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @ab: audit_buffer (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @va: audit struct to audit values of (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static void file_audit_cb(struct audit_buffer *ab, void *va)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct common_audit_data *sa = va;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) kuid_t fsuid = current_fsuid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) char str[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) aa_perm_mask_to_str(str, sizeof(str), aa_file_perm_chrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) map_mask_to_chr_mask(aad(sa)->request));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) audit_log_format(ab, " requested_mask=\"%s\"", str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (aad(sa)->denied & AA_AUDIT_FILE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) aa_perm_mask_to_str(str, sizeof(str), aa_file_perm_chrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) map_mask_to_chr_mask(aad(sa)->denied));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) audit_log_format(ab, " denied_mask=\"%s\"", str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) audit_log_format(ab, " fsuid=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) from_kuid(&init_user_ns, fsuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) audit_log_format(ab, " ouid=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) from_kuid(&init_user_ns, aad(sa)->fs.ouid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (aad(sa)->peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) audit_log_format(ab, " target=");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) FLAG_VIEW_SUBNS, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) } else if (aad(sa)->fs.target) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) audit_log_format(ab, " target=");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) audit_log_untrustedstring(ab, aad(sa)->fs.target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * aa_audit_file - handle the auditing of file operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * @profile: the profile being enforced (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * @perms: the permissions computed for the request (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @op: operation being mediated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @request: permissions requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * @name: name of object being mediated (MAYBE NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * @target: name of target (MAYBE NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * @tlabel: target label (MAY BE NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * @ouid: object uid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * @info: extra information message (MAYBE NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * @error: 0 if operation allowed else failure error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * Returns: %0 or error on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) const char *op, u32 request, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) const char *target, struct aa_label *tlabel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) kuid_t ouid, const char *info, int error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int type = AUDIT_APPARMOR_AUTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_TASK, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) sa.u.tsk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) aad(&sa)->request = request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) aad(&sa)->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) aad(&sa)->fs.target = target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) aad(&sa)->peer = tlabel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) aad(&sa)->fs.ouid = ouid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) aad(&sa)->info = info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) aad(&sa)->error = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) sa.u.tsk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (likely(!aad(&sa)->error)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) u32 mask = perms->audit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) mask = 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* mask off perms that are not being force audited */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) aad(&sa)->request &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (likely(!aad(&sa)->request))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) type = AUDIT_APPARMOR_AUDIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /* only report permissions that were denied */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) aad(&sa)->request = aad(&sa)->request & ~perms->allow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) AA_BUG(!aad(&sa)->request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (aad(&sa)->request & perms->kill)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) type = AUDIT_APPARMOR_KILL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* quiet known rejects, assumes quiet and kill do not overlap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if ((aad(&sa)->request & perms->quiet) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) AUDIT_MODE(profile) != AUDIT_NOQUIET &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) AUDIT_MODE(profile) != AUDIT_ALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) aad(&sa)->request &= ~perms->quiet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!aad(&sa)->request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return aad(&sa)->error;
^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) aad(&sa)->denied = aad(&sa)->request & ~perms->allow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return aa_audit(type, profile, &sa, file_audit_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * is_deleted - test if a file has been completely unlinked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * @dentry: dentry of file to test for deletion (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * Returns: true if deleted else false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static inline bool is_deleted(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (d_unlinked(dentry) && d_backing_inode(dentry)->i_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int path_name(const char *op, struct aa_label *label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) const struct path *path, int flags, char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) const char **name, struct path_cond *cond, u32 request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct aa_profile *profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) const char *info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) error = aa_path_name(path, flags, buffer, name, &info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) labels_profile(label)->disconnected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) fn_for_each_confined(label, profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) aa_audit_file(profile, &nullperms, op, request, *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) NULL, NULL, cond->uid, info, error));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return error;
^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) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * map_old_perms - map old file perms layout to the new layout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * @old: permission set in old mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * Returns: new permission mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static u32 map_old_perms(u32 old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) u32 new = old & 0xf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (old & MAY_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) new |= AA_MAY_GETATTR | AA_MAY_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (old & MAY_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) new |= AA_MAY_SETATTR | AA_MAY_CREATE | AA_MAY_DELETE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (old & 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) new |= AA_MAY_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* the old mapping lock and link_subset flags where overlaid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * and use was determined by part of a pair that they were in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (old & 0x20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) new |= AA_MAY_LOCK | AA_LINK_SUBSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (old & 0x40) /* AA_EXEC_MMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) new |= AA_EXEC_MMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^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) * aa_compute_fperms - convert dfa compressed perms to internal perms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * @dfa: dfa to compute perms for (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * @state: state in dfa
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * @cond: conditions to consider (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * TODO: convert from dfa + state to permission entry, do computation conversion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * at load time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * Returns: computed permission set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct path_cond *cond)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /* FIXME: change over to new dfa format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * currently file perms are encoded in the dfa, new format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * splits the permissions from the dfa. This mapping can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * done at profile load
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct aa_perms perms = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (uid_eq(current_fsuid(), cond->uid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) perms.allow = map_old_perms(dfa_user_allow(dfa, state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) perms.audit = map_old_perms(dfa_user_audit(dfa, state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) perms.quiet = map_old_perms(dfa_user_quiet(dfa, state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) perms.xindex = dfa_user_xindex(dfa, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) perms.allow = map_old_perms(dfa_other_allow(dfa, state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) perms.audit = map_old_perms(dfa_other_audit(dfa, state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) perms.quiet = map_old_perms(dfa_other_quiet(dfa, state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) perms.xindex = dfa_other_xindex(dfa, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) perms.allow |= AA_MAY_GETATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* change_profile wasn't determined by ownership in old mapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (ACCEPT_TABLE(dfa)[state] & 0x80000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) perms.allow |= AA_MAY_CHANGE_PROFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (ACCEPT_TABLE(dfa)[state] & 0x40000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) perms.allow |= AA_MAY_ONEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return perms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * aa_str_perms - find permission that match @name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * @dfa: to match against (MAYBE NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * @state: state to start matching in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * @name: string to match against dfa (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * @cond: conditions to consider for permission set computation (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * @perms: Returns - the permissions found when matching @name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * Returns: the final state in @dfa when beginning @start and walking @name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) const char *name, struct path_cond *cond,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct aa_perms *perms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) unsigned int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) state = aa_dfa_match(dfa, start, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) *perms = aa_compute_fperms(dfa, state, cond);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int __aa_path_perm(const char *op, struct aa_profile *profile, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) u32 request, struct path_cond *cond, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct aa_perms *perms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int e = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (profile_unconfined(profile))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) aa_str_perms(profile->file.dfa, profile->file.start, name, cond, perms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (request & ~perms->allow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) e = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return aa_audit_file(profile, perms, op, request, name, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) cond->uid, NULL, e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^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) static int profile_path_perm(const char *op, struct aa_profile *profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) const struct path *path, char *buffer, u32 request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct path_cond *cond, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct aa_perms *perms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (profile_unconfined(profile))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) error = path_name(op, &profile->label, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) flags | profile->path_flags, buffer, &name, cond,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return __aa_path_perm(op, profile, name, request, cond, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) perms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * aa_path_perm - do permissions check & audit for @path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * @op: operation being checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * @label: profile being enforced (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * @path: path to check permissions of (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * @flags: any additional path flags beyond what the profile specifies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * @request: requested permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * @cond: conditional info for this request (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * Returns: %0 else error if access denied or other error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) int aa_path_perm(const char *op, struct aa_label *label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) const struct path *path, int flags, u32 request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct path_cond *cond)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct aa_perms perms = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct aa_profile *profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) char *buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) flags |= PATH_DELEGATE_DELETED | (S_ISDIR(cond->mode) ? PATH_IS_DIR :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) buffer = aa_get_buffer(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) error = fn_for_each_confined(label, profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) profile_path_perm(op, profile, path, buffer, request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) cond, flags, &perms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) aa_put_buffer(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * xindex_is_subset - helper for aa_path_link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * @link: link permission set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * @target: target permission set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * test target x permissions are equal OR a subset of link x permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * this is done as part of the subset test, where a hardlink must have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * a subset of permissions that the target has.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * Returns: true if subset else false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static inline bool xindex_is_subset(u32 link, u32 target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (((link & ~AA_X_UNSAFE) != (target & ~AA_X_UNSAFE)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) ((link & AA_X_UNSAFE) && !(target & AA_X_UNSAFE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static int profile_path_link(struct aa_profile *profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) const struct path *link, char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) const struct path *target, char *buffer2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct path_cond *cond)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) const char *lname, *tname = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct aa_perms lperms = {}, perms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) const char *info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) u32 request = AA_MAY_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) unsigned int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) error = path_name(OP_LINK, &profile->label, link, profile->path_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) buffer, &lname, cond, AA_MAY_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) goto audit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /* buffer2 freed below, tname is pointer in buffer2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) error = path_name(OP_LINK, &profile->label, target, profile->path_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) buffer2, &tname, cond, AA_MAY_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) goto audit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) error = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* aa_str_perms - handles the case of the dfa being NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) state = aa_str_perms(profile->file.dfa, profile->file.start, lname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) cond, &lperms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (!(lperms.allow & AA_MAY_LINK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) goto audit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /* test to see if target can be paired with link */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) state = aa_dfa_null_transition(profile->file.dfa, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) aa_str_perms(profile->file.dfa, state, tname, cond, &perms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) /* force audit/quiet masks for link are stored in the second entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * in the link pair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) lperms.audit = perms.audit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) lperms.quiet = perms.quiet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) lperms.kill = perms.kill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (!(perms.allow & AA_MAY_LINK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) info = "target restricted";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) lperms = perms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) goto audit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) /* done if link subset test is not required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (!(perms.allow & AA_LINK_SUBSET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) goto done_tests;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /* Do link perm subset test requiring allowed permission on link are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * a subset of the allowed permissions on target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) aa_str_perms(profile->file.dfa, profile->file.start, tname, cond,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) &perms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) /* AA_MAY_LINK is not considered in the subset test */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) request = lperms.allow & ~AA_MAY_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) lperms.allow &= perms.allow | AA_MAY_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) request |= AA_AUDIT_FILE_MASK & (lperms.allow & ~perms.allow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (request & ~lperms.allow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) goto audit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) } else if ((lperms.allow & MAY_EXEC) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) !xindex_is_subset(lperms.xindex, perms.xindex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) lperms.allow &= ~MAY_EXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) request |= MAY_EXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) info = "link not subset of target";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) goto audit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) done_tests:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) audit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return aa_audit_file(profile, &lperms, OP_LINK, request, lname, tname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) NULL, cond->uid, info, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^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) * aa_path_link - Handle hard link permission check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * @label: the label being enforced (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * @old_dentry: the target dentry (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * @new_dir: directory the new link will be created in (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * @new_dentry: the link being created (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * Handle the permission test for a link & target pair. Permission
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * is encoded as a pair where the link permission is determined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * first, and if allowed, the target is tested. The target test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * is done from the point of the link match (not start of DFA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * making the target permission dependent on the link permission match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * The subset test if required forces that permissions granted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * on link are a subset of the permission granted to target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * Returns: %0 if allowed else error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) int aa_path_link(struct aa_label *label, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) const struct path *new_dir, struct dentry *new_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) struct path link = { .mnt = new_dir->mnt, .dentry = new_dentry };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) struct path target = { .mnt = new_dir->mnt, .dentry = old_dentry };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) struct path_cond cond = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) d_backing_inode(old_dentry)->i_uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) d_backing_inode(old_dentry)->i_mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) char *buffer = NULL, *buffer2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) struct aa_profile *profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /* buffer freed below, lname is pointer in buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) buffer = aa_get_buffer(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) buffer2 = aa_get_buffer(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (!buffer || !buffer2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) error = fn_for_each_confined(label, profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) profile_path_link(profile, &link, buffer, &target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) buffer2, &cond));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) aa_put_buffer(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) aa_put_buffer(buffer2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static void update_file_ctx(struct aa_file_ctx *fctx, struct aa_label *label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) u32 request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct aa_label *l, *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /* update caching of label on file_ctx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) spin_lock(&fctx->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) old = rcu_dereference_protected(fctx->label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) lockdep_is_held(&fctx->lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) l = aa_label_merge(old, label, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (l != old) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) rcu_assign_pointer(fctx->label, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) aa_put_label(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) aa_put_label(l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) fctx->allow |= request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) spin_unlock(&fctx->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) static int __file_path_perm(const char *op, struct aa_label *label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) struct aa_label *flabel, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) u32 request, u32 denied, bool in_atomic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) struct aa_profile *profile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct aa_perms perms = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) struct path_cond cond = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) .uid = file_inode(file)->i_uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) .mode = file_inode(file)->i_mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) int flags, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) /* revalidation due to label out of date. No revocation at this time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (!denied && aa_label_is_subset(flabel, label))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) /* TODO: check for revocation on stale profiles */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) flags = PATH_DELEGATE_DELETED | (S_ISDIR(cond.mode) ? PATH_IS_DIR : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) buffer = aa_get_buffer(in_atomic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) /* check every profile in task label not in current cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) error = fn_for_each_not_in_set(flabel, label, profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) profile_path_perm(op, profile, &file->f_path, buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) request, &cond, flags, &perms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (denied && !error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * check every profile in file label that was not tested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * in the initial check above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * TODO: cache full perms so this only happens because of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * conditionals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) * TODO: don't audit here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (label == flabel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) error = fn_for_each(label, profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) profile_path_perm(op, profile, &file->f_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) buffer, request, &cond, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) &perms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) error = fn_for_each_not_in_set(label, flabel, profile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) profile_path_perm(op, profile, &file->f_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) buffer, request, &cond, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) &perms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) update_file_ctx(file_ctx(file), label, request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) aa_put_buffer(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) static int __file_sock_perm(const char *op, struct aa_label *label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) struct aa_label *flabel, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) u32 request, u32 denied)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) struct socket *sock = (struct socket *) file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) AA_BUG(!sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /* revalidation due to label out of date. No revocation at this time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (!denied && aa_label_is_subset(flabel, label))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) /* TODO: improve to skip profiles cached in flabel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) error = aa_sock_file_perm(label, op, request, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (denied) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /* TODO: improve to skip profiles checked above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) /* check every profile in file label to is cached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) last_error(error, aa_sock_file_perm(flabel, op, request, sock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) update_file_ctx(file_ctx(file), label, request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * aa_file_perm - do permission revalidation check & audit for @file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * @op: operation being checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * @label: label being enforced (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * @file: file to revalidate access permissions on (NOT NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * @request: requested permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * @in_atomic: whether allocations need to be done in atomic context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) * Returns: %0 if access allowed else error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) u32 request, bool in_atomic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) struct aa_file_ctx *fctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) struct aa_label *flabel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) u32 denied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) AA_BUG(!label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) AA_BUG(!file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) fctx = file_ctx(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) flabel = rcu_dereference(fctx->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) AA_BUG(!flabel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) /* revalidate access, if task is unconfined, or the cached cred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * doesn't match or if the request is for more permissions than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) * was granted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * Note: the test for !unconfined(flabel) is to handle file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * delegation from unconfined tasks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) denied = request & ~fctx->allow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (unconfined(label) || unconfined(flabel) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) (!denied && aa_label_is_subset(flabel, label))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) flabel = aa_get_newest_label(flabel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) /* TODO: label cross check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (file->f_path.mnt && path_mediated_fs(file->f_path.dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) error = __file_path_perm(op, label, flabel, file, request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) denied, in_atomic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) else if (S_ISSOCK(file_inode(file)->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) error = __file_sock_perm(op, label, flabel, file, request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) denied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) aa_put_label(flabel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) static void revalidate_tty(struct aa_label *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) struct tty_struct *tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) int drop_tty = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) tty = get_current_tty();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (!tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) spin_lock(&tty->files_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (!list_empty(&tty->tty_files)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) struct tty_file_private *file_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) /* TODO: Revalidate access to controlling tty. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) file_priv = list_first_entry(&tty->tty_files,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) struct tty_file_private, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) file = file_priv->file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if (aa_file_perm(OP_INHERIT, label, file, MAY_READ | MAY_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) IN_ATOMIC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) drop_tty = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) spin_unlock(&tty->files_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) tty_kref_put(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) if (drop_tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) no_tty();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) static int match_file(const void *p, struct file *file, unsigned int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) struct aa_label *label = (struct aa_label *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (aa_file_perm(OP_INHERIT, label, file, aa_map_file_to_perms(file),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) IN_ATOMIC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) return fd + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) /* based on selinux's flush_unauthorized_files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) void aa_inherit_files(const struct cred *cred, struct files_struct *files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) struct aa_label *label = aa_get_newest_cred_label(cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) struct file *devnull = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) unsigned int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) revalidate_tty(label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) /* Revalidate access to inherited open files. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) n = iterate_fd(files, 0, match_file, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (!n) /* none found? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) devnull = dentry_open(&aa_null, O_RDWR, cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if (IS_ERR(devnull))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) devnull = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) /* replace all the matching ones with this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) replace_fd(n - 1, devnull, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) } while ((n = iterate_fd(files, n, match_file, label)) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (devnull)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) fput(devnull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) aa_put_label(label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }