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 resource mediation and attachment
0006  *
0007  * Copyright (C) 1998-2008 Novell/SUSE
0008  * Copyright 2009-2010 Canonical Ltd.
0009  */
0010 
0011 #include <linux/audit.h>
0012 #include <linux/security.h>
0013 
0014 #include "include/audit.h"
0015 #include "include/cred.h"
0016 #include "include/resource.h"
0017 #include "include/policy.h"
0018 
0019 /*
0020  * Table of rlimit names: we generate it from resource.h.
0021  */
0022 #include "rlim_names.h"
0023 
0024 struct aa_sfs_entry aa_sfs_entry_rlimit[] = {
0025     AA_SFS_FILE_STRING("mask", AA_SFS_RLIMIT_MASK),
0026     { }
0027 };
0028 
0029 /* audit callback for resource specific fields */
0030 static void audit_cb(struct audit_buffer *ab, void *va)
0031 {
0032     struct common_audit_data *sa = va;
0033 
0034     audit_log_format(ab, " rlimit=%s value=%lu",
0035              rlim_names[aad(sa)->rlim.rlim], aad(sa)->rlim.max);
0036     if (aad(sa)->peer) {
0037         audit_log_format(ab, " peer=");
0038         aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
0039                 FLAGS_NONE, GFP_ATOMIC);
0040     }
0041 }
0042 
0043 /**
0044  * audit_resource - audit setting resource limit
0045  * @profile: profile being enforced  (NOT NULL)
0046  * @resource: rlimit being auditing
0047  * @value: value being set
0048  * @error: error value
0049  *
0050  * Returns: 0 or sa->error else other error code on failure
0051  */
0052 static int audit_resource(struct aa_profile *profile, unsigned int resource,
0053               unsigned long value, struct aa_label *peer,
0054               const char *info, int error)
0055 {
0056     DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETRLIMIT);
0057 
0058     aad(&sa)->rlim.rlim = resource;
0059     aad(&sa)->rlim.max = value;
0060     aad(&sa)->peer = peer;
0061     aad(&sa)->info = info;
0062     aad(&sa)->error = error;
0063 
0064     return aa_audit(AUDIT_APPARMOR_AUTO, profile, &sa, audit_cb);
0065 }
0066 
0067 /**
0068  * aa_map_resouce - map compiled policy resource to internal #
0069  * @resource: flattened policy resource number
0070  *
0071  * Returns: resource # for the current architecture.
0072  *
0073  * rlimit resource can vary based on architecture, map the compiled policy
0074  * resource # to the internal representation for the architecture.
0075  */
0076 int aa_map_resource(int resource)
0077 {
0078     return rlim_map[resource];
0079 }
0080 
0081 static int profile_setrlimit(struct aa_profile *profile, unsigned int resource,
0082                  struct rlimit *new_rlim)
0083 {
0084     int e = 0;
0085 
0086     if (profile->rlimits.mask & (1 << resource) && new_rlim->rlim_max >
0087         profile->rlimits.limits[resource].rlim_max)
0088         e = -EACCES;
0089     return audit_resource(profile, resource, new_rlim->rlim_max, NULL, NULL,
0090                   e);
0091 }
0092 
0093 /**
0094  * aa_task_setrlimit - test permission to set an rlimit
0095  * @label - label confining the task  (NOT NULL)
0096  * @task - task the resource is being set on
0097  * @resource - the resource being set
0098  * @new_rlim - the new resource limit  (NOT NULL)
0099  *
0100  * Control raising the processes hard limit.
0101  *
0102  * Returns: 0 or error code if setting resource failed
0103  */
0104 int aa_task_setrlimit(struct aa_label *label, struct task_struct *task,
0105               unsigned int resource, struct rlimit *new_rlim)
0106 {
0107     struct aa_profile *profile;
0108     struct aa_label *peer;
0109     int error = 0;
0110 
0111     rcu_read_lock();
0112     peer = aa_get_newest_cred_label(__task_cred(task));
0113     rcu_read_unlock();
0114 
0115     /* TODO: extend resource control to handle other (non current)
0116      * profiles.  AppArmor rules currently have the implicit assumption
0117      * that the task is setting the resource of a task confined with
0118      * the same profile or that the task setting the resource of another
0119      * task has CAP_SYS_RESOURCE.
0120      */
0121 
0122     if (label != peer &&
0123         aa_capable(label, CAP_SYS_RESOURCE, CAP_OPT_NOAUDIT) != 0)
0124         error = fn_for_each(label, profile,
0125                 audit_resource(profile, resource,
0126                            new_rlim->rlim_max, peer,
0127                            "cap_sys_resource", -EACCES));
0128     else
0129         error = fn_for_each_confined(label, profile,
0130                 profile_setrlimit(profile, resource, new_rlim));
0131     aa_put_label(peer);
0132 
0133     return error;
0134 }
0135 
0136 /**
0137  * __aa_transition_rlimits - apply new profile rlimits
0138  * @old_l: old label on task  (NOT NULL)
0139  * @new_l: new label with rlimits to apply  (NOT NULL)
0140  */
0141 void __aa_transition_rlimits(struct aa_label *old_l, struct aa_label *new_l)
0142 {
0143     unsigned int mask = 0;
0144     struct rlimit *rlim, *initrlim;
0145     struct aa_profile *old, *new;
0146     struct label_it i;
0147 
0148     old = labels_profile(old_l);
0149     new = labels_profile(new_l);
0150 
0151     /* for any rlimits the profile controlled, reset the soft limit
0152      * to the lesser of the tasks hard limit and the init tasks soft limit
0153      */
0154     label_for_each_confined(i, old_l, old) {
0155         if (old->rlimits.mask) {
0156             int j;
0157 
0158             for (j = 0, mask = 1; j < RLIM_NLIMITS; j++,
0159                      mask <<= 1) {
0160                 if (old->rlimits.mask & mask) {
0161                     rlim = current->signal->rlim + j;
0162                     initrlim = init_task.signal->rlim + j;
0163                     rlim->rlim_cur = min(rlim->rlim_max,
0164                                 initrlim->rlim_cur);
0165                 }
0166             }
0167         }
0168     }
0169 
0170     /* set any new hard limits as dictated by the new profile */
0171     label_for_each_confined(i, new_l, new) {
0172         int j;
0173 
0174         if (!new->rlimits.mask)
0175             continue;
0176         for (j = 0, mask = 1; j < RLIM_NLIMITS; j++, mask <<= 1) {
0177             if (!(new->rlimits.mask & mask))
0178                 continue;
0179 
0180             rlim = current->signal->rlim + j;
0181             rlim->rlim_max = min(rlim->rlim_max,
0182                          new->rlimits.limits[j].rlim_max);
0183             /* soft limit should not exceed hard limit */
0184             rlim->rlim_cur = min(rlim->rlim_cur, rlim->rlim_max);
0185         }
0186     }
0187 }