Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * AppArmor security module
0004  *
0005  * This file contains AppArmor file mediation function definitions.
0006  *
0007  * Copyright (C) 1998-2008 Novell/SUSE
0008  * Copyright 2009-2010 Canonical Ltd.
0009  */
0010 
0011 #ifndef __AA_FILE_H
0012 #define __AA_FILE_H
0013 
0014 #include <linux/spinlock.h>
0015 
0016 #include "domain.h"
0017 #include "match.h"
0018 #include "perms.h"
0019 
0020 struct aa_profile;
0021 struct path;
0022 
0023 #define mask_mode_t(X) (X & (MAY_EXEC | MAY_WRITE | MAY_READ | MAY_APPEND))
0024 
0025 #define AA_AUDIT_FILE_MASK  (MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\
0026                  AA_MAY_CREATE | AA_MAY_DELETE |    \
0027                  AA_MAY_GETATTR | AA_MAY_SETATTR | \
0028                  AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
0029                  AA_EXEC_MMAP | AA_MAY_LINK)
0030 
0031 static inline struct aa_file_ctx *file_ctx(struct file *file)
0032 {
0033     return file->f_security + apparmor_blob_sizes.lbs_file;
0034 }
0035 
0036 /* struct aa_file_ctx - the AppArmor context the file was opened in
0037  * @lock: lock to update the ctx
0038  * @label: label currently cached on the ctx
0039  * @perms: the permission the file was opened with
0040  */
0041 struct aa_file_ctx {
0042     spinlock_t lock;
0043     struct aa_label __rcu *label;
0044     u32 allow;
0045 };
0046 
0047 /**
0048  * aa_alloc_file_ctx - allocate file_ctx
0049  * @label: initial label of task creating the file
0050  * @gfp: gfp flags for allocation
0051  *
0052  * Returns: file_ctx or NULL on failure
0053  */
0054 static inline struct aa_file_ctx *aa_alloc_file_ctx(struct aa_label *label,
0055                             gfp_t gfp)
0056 {
0057     struct aa_file_ctx *ctx;
0058 
0059     ctx = kzalloc(sizeof(struct aa_file_ctx), gfp);
0060     if (ctx) {
0061         spin_lock_init(&ctx->lock);
0062         rcu_assign_pointer(ctx->label, aa_get_label(label));
0063     }
0064     return ctx;
0065 }
0066 
0067 /**
0068  * aa_free_file_ctx - free a file_ctx
0069  * @ctx: file_ctx to free  (MAYBE_NULL)
0070  */
0071 static inline void aa_free_file_ctx(struct aa_file_ctx *ctx)
0072 {
0073     if (ctx) {
0074         aa_put_label(rcu_access_pointer(ctx->label));
0075         kfree_sensitive(ctx);
0076     }
0077 }
0078 
0079 static inline struct aa_label *aa_get_file_label(struct aa_file_ctx *ctx)
0080 {
0081     return aa_get_label_rcu(&ctx->label);
0082 }
0083 
0084 /*
0085  * The xindex is broken into 3 parts
0086  * - index - an index into either the exec name table or the variable table
0087  * - exec type - which determines how the executable name and index are used
0088  * - flags - which modify how the destination name is applied
0089  */
0090 #define AA_X_INDEX_MASK     0x03ff
0091 
0092 #define AA_X_TYPE_MASK      0x0c00
0093 #define AA_X_TYPE_SHIFT     10
0094 #define AA_X_NONE       0x0000
0095 #define AA_X_NAME       0x0400  /* use executable name px */
0096 #define AA_X_TABLE      0x0800  /* use a specified name ->n# */
0097 
0098 #define AA_X_UNSAFE     0x1000
0099 #define AA_X_CHILD      0x2000  /* make >AA_X_NONE apply to children */
0100 #define AA_X_INHERIT        0x4000
0101 #define AA_X_UNCONFINED     0x8000
0102 
0103 /* need to make conditional which ones are being set */
0104 struct path_cond {
0105     kuid_t uid;
0106     umode_t mode;
0107 };
0108 
0109 #define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill)
0110 
0111 /* FIXME: split perms from dfa and match this to description
0112  *        also add delegation info.
0113  */
0114 static inline u16 dfa_map_xindex(u16 mask)
0115 {
0116     u16 old_index = (mask >> 10) & 0xf;
0117     u16 index = 0;
0118 
0119     if (mask & 0x100)
0120         index |= AA_X_UNSAFE;
0121     if (mask & 0x200)
0122         index |= AA_X_INHERIT;
0123     if (mask & 0x80)
0124         index |= AA_X_UNCONFINED;
0125 
0126     if (old_index == 1) {
0127         index |= AA_X_UNCONFINED;
0128     } else if (old_index == 2) {
0129         index |= AA_X_NAME;
0130     } else if (old_index == 3) {
0131         index |= AA_X_NAME | AA_X_CHILD;
0132     } else if (old_index) {
0133         index |= AA_X_TABLE;
0134         index |= old_index - 4;
0135     }
0136 
0137     return index;
0138 }
0139 
0140 /*
0141  * map old dfa inline permissions to new format
0142  */
0143 #define dfa_user_allow(dfa, state) (((ACCEPT_TABLE(dfa)[state]) & 0x7f) | \
0144                     ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
0145 #define dfa_user_xbits(dfa, state) (((ACCEPT_TABLE(dfa)[state]) >> 7) & 0x7f)
0146 #define dfa_user_audit(dfa, state) ((ACCEPT_TABLE2(dfa)[state]) & 0x7f)
0147 #define dfa_user_quiet(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 7) & 0x7f)
0148 #define dfa_user_xindex(dfa, state) \
0149     (dfa_map_xindex(ACCEPT_TABLE(dfa)[state] & 0x3fff))
0150 
0151 #define dfa_other_allow(dfa, state) ((((ACCEPT_TABLE(dfa)[state]) >> 14) & \
0152                       0x7f) |               \
0153                      ((ACCEPT_TABLE(dfa)[state]) & 0x80000000))
0154 #define dfa_other_xbits(dfa, state) \
0155     ((((ACCEPT_TABLE(dfa)[state]) >> 7) >> 14) & 0x7f)
0156 #define dfa_other_audit(dfa, state) (((ACCEPT_TABLE2(dfa)[state]) >> 14) & 0x7f)
0157 #define dfa_other_quiet(dfa, state) \
0158     ((((ACCEPT_TABLE2(dfa)[state]) >> 7) >> 14) & 0x7f)
0159 #define dfa_other_xindex(dfa, state) \
0160     dfa_map_xindex((ACCEPT_TABLE(dfa)[state] >> 14) & 0x3fff)
0161 
0162 int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
0163           const char *op, u32 request, const char *name,
0164           const char *target, struct aa_label *tlabel, kuid_t ouid,
0165           const char *info, int error);
0166 
0167 /**
0168  * struct aa_file_rules - components used for file rule permissions
0169  * @dfa: dfa to match path names and conditionals against
0170  * @perms: permission table indexed by the matched state accept entry of @dfa
0171  * @trans: transition table for indexed by named x transitions
0172  *
0173  * File permission are determined by matching a path against @dfa and
0174  * then using the value of the accept entry for the matching state as
0175  * an index into @perms.  If a named exec transition is required it is
0176  * looked up in the transition table.
0177  */
0178 struct aa_file_rules {
0179     unsigned int start;
0180     struct aa_dfa *dfa;
0181     /* struct perms perms; */
0182     struct aa_domain trans;
0183     /* TODO: add delegate table */
0184 };
0185 
0186 struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
0187                     struct path_cond *cond);
0188 unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
0189               const char *name, struct path_cond *cond,
0190               struct aa_perms *perms);
0191 
0192 int __aa_path_perm(const char *op, struct aa_profile *profile,
0193            const char *name, u32 request, struct path_cond *cond,
0194            int flags, struct aa_perms *perms);
0195 int aa_path_perm(const char *op, struct aa_label *label,
0196          const struct path *path, int flags, u32 request,
0197          struct path_cond *cond);
0198 
0199 int aa_path_link(struct aa_label *label, struct dentry *old_dentry,
0200          const struct path *new_dir, struct dentry *new_dentry);
0201 
0202 int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
0203          u32 request, bool in_atomic);
0204 
0205 void aa_inherit_files(const struct cred *cred, struct files_struct *files);
0206 
0207 static inline void aa_free_file_rules(struct aa_file_rules *rules)
0208 {
0209     aa_put_dfa(rules->dfa);
0210     aa_free_domain_entries(&rules->trans);
0211 }
0212 
0213 /**
0214  * aa_map_file_perms - map file flags to AppArmor permissions
0215  * @file: open file to map flags to AppArmor permissions
0216  *
0217  * Returns: apparmor permission set for the file
0218  */
0219 static inline u32 aa_map_file_to_perms(struct file *file)
0220 {
0221     int flags = file->f_flags;
0222     u32 perms = 0;
0223 
0224     if (file->f_mode & FMODE_WRITE)
0225         perms |= MAY_WRITE;
0226     if (file->f_mode & FMODE_READ)
0227         perms |= MAY_READ;
0228 
0229     if ((flags & O_APPEND) && (perms & MAY_WRITE))
0230         perms = (perms & ~MAY_WRITE) | MAY_APPEND;
0231     /* trunc implies write permission */
0232     if (flags & O_TRUNC)
0233         perms |= MAY_WRITE;
0234     if (flags & O_CREAT)
0235         perms |= AA_MAY_CREATE;
0236 
0237     return perms;
0238 }
0239 
0240 #endif /* __AA_FILE_H */