/*1* AppArmor security module2*3* This file contains AppArmor resource mediation and attachment4*5* Copyright (C) 1998-2008 Novell/SUSE6* Copyright 2009-2010 Canonical Ltd.7*8* This program is free software; you can redistribute it and/or9* modify it under the terms of the GNU General Public License as10* published by the Free Software Foundation, version 2 of the11* License.12*/1314#include <linux/audit.h>1516#include "include/audit.h"17#include "include/resource.h"18#include "include/policy.h"1920/*21* Table of rlimit names: we generate it from resource.h.22*/23#include "rlim_names.h"2425/* audit callback for resource specific fields */26static void audit_cb(struct audit_buffer *ab, void *va)27{28struct common_audit_data *sa = va;2930audit_log_format(ab, " rlimit=%s value=%lu",31rlim_names[sa->aad.rlim.rlim], sa->aad.rlim.max);32}3334/**35* audit_resource - audit setting resource limit36* @profile: profile being enforced (NOT NULL)37* @resoure: rlimit being auditing38* @value: value being set39* @error: error value40*41* Returns: 0 or sa->error else other error code on failure42*/43static int audit_resource(struct aa_profile *profile, unsigned int resource,44unsigned long value, int error)45{46struct common_audit_data sa;4748COMMON_AUDIT_DATA_INIT(&sa, NONE);49sa.aad.op = OP_SETRLIMIT,50sa.aad.rlim.rlim = resource;51sa.aad.rlim.max = value;52sa.aad.error = error;53return aa_audit(AUDIT_APPARMOR_AUTO, profile, GFP_KERNEL, &sa,54audit_cb);55}5657/**58* aa_map_resouce - map compiled policy resource to internal #59* @resource: flattened policy resource number60*61* Returns: resource # for the current architecture.62*63* rlimit resource can vary based on architecture, map the compiled policy64* resource # to the internal representation for the architecture.65*/66int aa_map_resource(int resource)67{68return rlim_map[resource];69}7071/**72* aa_task_setrlimit - test permission to set an rlimit73* @profile - profile confining the task (NOT NULL)74* @task - task the resource is being set on75* @resource - the resource being set76* @new_rlim - the new resource limit (NOT NULL)77*78* Control raising the processes hard limit.79*80* Returns: 0 or error code if setting resource failed81*/82int aa_task_setrlimit(struct aa_profile *profile, struct task_struct *task,83unsigned int resource, struct rlimit *new_rlim)84{85int error = 0;8687/* TODO: extend resource control to handle other (non current)88* processes. AppArmor rules currently have the implicit assumption89* that the task is setting the resource of the current process90*/91if ((task != current->group_leader) ||92(profile->rlimits.mask & (1 << resource) &&93new_rlim->rlim_max > profile->rlimits.limits[resource].rlim_max))94error = -EACCES;9596return audit_resource(profile, resource, new_rlim->rlim_max, error);97}9899/**100* __aa_transition_rlimits - apply new profile rlimits101* @old: old profile on task (NOT NULL)102* @new: new profile with rlimits to apply (NOT NULL)103*/104void __aa_transition_rlimits(struct aa_profile *old, struct aa_profile *new)105{106unsigned int mask = 0;107struct rlimit *rlim, *initrlim;108int i;109110/* for any rlimits the profile controlled reset the soft limit111* to the less of the tasks hard limit and the init tasks soft limit112*/113if (old->rlimits.mask) {114for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) {115if (old->rlimits.mask & mask) {116rlim = current->signal->rlim + i;117initrlim = init_task.signal->rlim + i;118rlim->rlim_cur = min(rlim->rlim_max,119initrlim->rlim_cur);120}121}122}123124/* set any new hard limits as dictated by the new profile */125if (!new->rlimits.mask)126return;127for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) {128if (!(new->rlimits.mask & mask))129continue;130131rlim = current->signal->rlim + i;132rlim->rlim_max = min(rlim->rlim_max,133new->rlimits.limits[i].rlim_max);134/* soft limit should not exceed hard limit */135rlim->rlim_cur = min(rlim->rlim_cur, rlim->rlim_max);136}137}138139140