Commit | Line | Data |
---|---|---|
0ed3b28a JJ |
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 | ||
d384b0a1 KC |
26 | struct aa_fs_entry aa_fs_entry_rlimit[] = { |
27 | AA_FS_FILE_STRING("mask", AA_FS_RLIMIT_MASK), | |
28 | { } | |
29 | }; | |
30 | ||
0ed3b28a JJ |
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 | ||
40 | /** | |
41 | * audit_resource - audit setting resource limit | |
42 | * @profile: profile being enforced (NOT NULL) | |
43 | * @resoure: rlimit being auditing | |
44 | * @value: value being set | |
45 | * @error: error value | |
46 | * | |
47 | * Returns: 0 or sa->error else other error code on failure | |
48 | */ | |
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 | ||
54 | COMMON_AUDIT_DATA_INIT(&sa, NONE); | |
55 | sa.aad.op = OP_SETRLIMIT, | |
56 | sa.aad.rlim.rlim = resource; | |
57 | sa.aad.rlim.max = value; | |
58 | sa.aad.error = error; | |
59 | return aa_audit(AUDIT_APPARMOR_AUTO, profile, GFP_KERNEL, &sa, | |
60 | audit_cb); | |
61 | } | |
62 | ||
63 | /** | |
64 | * aa_map_resouce - map compiled policy resource to internal # | |
65 | * @resource: flattened policy resource number | |
66 | * | |
67 | * Returns: resource # for the current architecture. | |
68 | * | |
69 | * rlimit resource can vary based on architecture, map the compiled policy | |
70 | * resource # to the internal representation for the architecture. | |
71 | */ | |
72 | int aa_map_resource(int resource) | |
73 | { | |
74 | return rlim_map[resource]; | |
75 | } | |
76 | ||
77 | /** | |
78 | * aa_task_setrlimit - test permission to set an rlimit | |
79 | * @profile - profile confining the task (NOT NULL) | |
3a2dc838 | 80 | * @task - task the resource is being set on |
0ed3b28a JJ |
81 | * @resource - the resource being set |
82 | * @new_rlim - the new resource limit (NOT NULL) | |
83 | * | |
84 | * Control raising the processes hard limit. | |
85 | * | |
86 | * Returns: 0 or error code if setting resource failed | |
87 | */ | |
3a2dc838 JJ |
88 | int aa_task_setrlimit(struct aa_profile *profile, struct task_struct *task, |
89 | unsigned int resource, struct rlimit *new_rlim) | |
0ed3b28a JJ |
90 | { |
91 | int error = 0; | |
92 | ||
3a2dc838 JJ |
93 | /* TODO: extend resource control to handle other (non current) |
94 | * processes. AppArmor rules currently have the implicit assumption | |
95 | * that the task is setting the resource of the current process | |
96 | */ | |
97 | if ((task != current->group_leader) || | |
98 | (profile->rlimits.mask & (1 << resource) && | |
99 | new_rlim->rlim_max > profile->rlimits.limits[resource].rlim_max)) | |
100 | error = -EACCES; | |
0ed3b28a | 101 | |
3a2dc838 | 102 | return audit_resource(profile, resource, new_rlim->rlim_max, error); |
0ed3b28a JJ |
103 | } |
104 | ||
105 | /** | |
106 | * __aa_transition_rlimits - apply new profile rlimits | |
107 | * @old: old profile on task (NOT NULL) | |
108 | * @new: new profile with rlimits to apply (NOT NULL) | |
109 | */ | |
110 | void __aa_transition_rlimits(struct aa_profile *old, struct aa_profile *new) | |
111 | { | |
112 | unsigned int mask = 0; | |
113 | struct rlimit *rlim, *initrlim; | |
114 | int i; | |
115 | ||
116 | /* for any rlimits the profile controlled reset the soft limit | |
117 | * to the less of the tasks hard limit and the init tasks soft limit | |
118 | */ | |
119 | if (old->rlimits.mask) { | |
120 | for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) { | |
121 | if (old->rlimits.mask & mask) { | |
122 | rlim = current->signal->rlim + i; | |
123 | initrlim = init_task.signal->rlim + i; | |
124 | rlim->rlim_cur = min(rlim->rlim_max, | |
125 | initrlim->rlim_cur); | |
126 | } | |
127 | } | |
128 | } | |
129 | ||
130 | /* set any new hard limits as dictated by the new profile */ | |
131 | if (!new->rlimits.mask) | |
132 | return; | |
133 | for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) { | |
134 | if (!(new->rlimits.mask & mask)) | |
135 | continue; | |
136 | ||
137 | rlim = current->signal->rlim + i; | |
138 | rlim->rlim_max = min(rlim->rlim_max, | |
139 | new->rlimits.limits[i].rlim_max); | |
140 | /* soft limit should not exceed hard limit */ | |
141 | rlim->rlim_cur = min(rlim->rlim_cur, rlim->rlim_max); | |
142 | } | |
143 | } |