Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
resource.c
Go to the documentation of this file.
1 /*
2  * AppArmor security module
3  *
4  * This file contains AppArmor resource mediation and attachment
5  *
6  * Copyright (C) 1998-2008 Novell/SUSE
7  * Copyright 2009-2010 Canonical Ltd.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation, version 2 of the
12  * License.
13  */
14 
15 #include <linux/audit.h>
16 
17 #include "include/audit.h"
18 #include "include/resource.h"
19 #include "include/policy.h"
20 
21 /*
22  * Table of rlimit names: we generate it from resource.h.
23  */
24 #include "rlim_names.h"
25 
27  AA_FS_FILE_STRING("mask", AA_FS_RLIMIT_MASK),
28  { }
29 };
30 
31 /* audit callback for resource specific fields */
32 static void audit_cb(struct audit_buffer *ab, void *va)
33 {
34  struct common_audit_data *sa = va;
35 
36  audit_log_format(ab, " rlimit=%s value=%lu",
37  rlim_names[sa->aad->rlim.rlim], sa->aad->rlim.max);
38 }
39 
49 static int audit_resource(struct aa_profile *profile, unsigned int resource,
50  unsigned long value, int error)
51 {
52  struct common_audit_data sa;
53  struct apparmor_audit_data aad = {0,};
54 
55  sa.type = LSM_AUDIT_DATA_NONE;
56  sa.aad = &aad;
57  aad.op = OP_SETRLIMIT,
58  aad.rlim.rlim = resource;
59  aad.rlim.max = value;
60  aad.error = error;
61  return aa_audit(AUDIT_APPARMOR_AUTO, profile, GFP_KERNEL, &sa,
62  audit_cb);
63 }
64 
75 {
76  return rlim_map[resource];
77 }
78 
91  unsigned int resource, struct rlimit *new_rlim)
92 {
93  int error = 0;
94 
95  /* TODO: extend resource control to handle other (non current)
96  * processes. AppArmor rules currently have the implicit assumption
97  * that the task is setting the resource of the current process
98  */
99  if ((task != current->group_leader) ||
100  (profile->rlimits.mask & (1 << resource) &&
101  new_rlim->rlim_max > profile->rlimits.limits[resource].rlim_max))
102  error = -EACCES;
103 
104  return audit_resource(profile, resource, new_rlim->rlim_max, error);
105 }
106 
112 void __aa_transition_rlimits(struct aa_profile *old, struct aa_profile *new)
113 {
114  unsigned int mask = 0;
115  struct rlimit *rlim, *initrlim;
116  int i;
117 
118  /* for any rlimits the profile controlled reset the soft limit
119  * to the less of the tasks hard limit and the init tasks soft limit
120  */
121  if (old->rlimits.mask) {
122  for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) {
123  if (old->rlimits.mask & mask) {
124  rlim = current->signal->rlim + i;
125  initrlim = init_task.signal->rlim + i;
126  rlim->rlim_cur = min(rlim->rlim_max,
127  initrlim->rlim_cur);
128  }
129  }
130  }
131 
132  /* set any new hard limits as dictated by the new profile */
133  if (!new->rlimits.mask)
134  return;
135  for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) {
136  if (!(new->rlimits.mask & mask))
137  continue;
138 
139  rlim = current->signal->rlim + i;
140  rlim->rlim_max = min(rlim->rlim_max,
141  new->rlimits.limits[i].rlim_max);
142  /* soft limit should not exceed hard limit */
143  rlim->rlim_cur = min(rlim->rlim_cur, rlim->rlim_max);
144  }
145 }