libamxrt  0.4.2
Ambiorix Run Time Library
test_amxrt_caps.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <setjmp.h>
58 #include <stdarg.h>
59 #include <cmocka.h>
60 #include <cap-ng.h>
61 #include <pwd.h>
62 #include <grp.h>
63 
64 #include "test_amxrt_caps.h"
65 
68 void __wrap_capng_clear(capng_select_t set);
69 int __wrap_capng_update(capng_act_t action, capng_type_t type, unsigned int capability);
70 int __wrap_capng_apply(capng_select_t set);
71 int __wrap_capng_change_id(int uid, int gid, capng_flags_t flag);
72 struct passwd* __wrap_getpwnam(const char* __name);
73 struct group* __wrap_getgrnam(const char* __name);
74 
76  return 0;
77 }
78 
79 void __wrap_capng_clear(UNUSED capng_select_t set) {
80 
81 }
82 
83 int __wrap_capng_update(UNUSED capng_act_t action, UNUSED capng_type_t type, UNUSED unsigned int capability) {
84  return mock();
85 }
86 
87 int __wrap_capng_apply(UNUSED capng_select_t set) {
88  return mock();
89 }
90 
91 int __wrap_capng_change_id(UNUSED int uid, UNUSED int gid, UNUSED capng_flags_t flag) {
92  return 0;
93 }
94 
95 struct passwd* __wrap_getpwnam(const char* name) {
96  static struct passwd pwd;
97  pwd.pw_uid = 1000;
98 
99  check_expected(name);
100 
101  if(strcmp(name, "webadmin") == 0) {
102  return &pwd;
103  } else {
104  return NULL;
105  }
106 }
107 
108 struct group* __wrap_getgrnam(const char* name) {
109  static struct group grp;
110  grp.gr_gid = 100;
111 
112  check_expected(name);
113 
114  if(strcmp(name, "webui") == 0) {
115  return &grp;
116  } else {
117  return NULL;
118  }
119 }
120 
121 int test_caps_setup(UNUSED void** state) {
122  amxrt_new();
123  return 0;
124 }
125 
126 int test_caps_teardown(UNUSED void** state) {
127  amxrt_delete();
128  return 0;
129 }
130 
131 void test_caps_switch_user_group(UNUSED void** state) {
132  amxc_var_t* config = amxrt_get_config();
133  amxc_var_t* privileges = amxc_var_add_key(amxc_htable_t, config, "privileges", NULL);
134 
135  amxc_var_add_key(cstring_t, privileges, "user", "webadmin");
136  amxc_var_add_key(cstring_t, privileges, "group", "webui");
137 
138  expect_string(__wrap_getpwnam, name, "webadmin");
139  expect_string(__wrap_getgrnam, name, "webui");
140 
141  assert_int_equal(amxrt_caps_apply(), 0);
142 
143  amxc_var_delete(&privileges);
144 }
145 
146 void test_caps_use_null_user_group(UNUSED void** state) {
147  amxc_var_t* config = amxrt_get_config();
148  amxc_var_t* privileges = amxc_var_add_key(amxc_htable_t, config, "privileges", NULL);
149  amxc_var_t* group = amxc_var_add_new_key(privileges, "group");
150 
151  amxc_var_add_new_key(privileges, "user");
152 
153  assert_int_equal(amxrt_caps_apply(), 0);
154 
155  amxc_var_set(cstring_t, group, "webui");
156  expect_string(__wrap_getgrnam, name, "webui");
157 
158  assert_int_equal(amxrt_caps_apply(), 0);
159 
160  amxc_var_delete(&privileges);
161 }
162 
163 void test_caps_use_non_existing_user_group(UNUSED void** state) {
164  amxc_var_t* config = amxrt_get_config();
165  amxc_var_t* privileges = amxc_var_add_key(amxc_htable_t, config, "privileges", NULL);
166 
167  amxc_var_add_key(cstring_t, privileges, "user", "non-existing");
168  amxc_var_add_key(cstring_t, privileges, "group", "fake-group");
169 
170  expect_string(__wrap_getpwnam, name, "non-existing");
171  expect_string(__wrap_getgrnam, name, "fake-group");
172 
173  assert_int_equal(amxrt_caps_apply(), 0);
174 
175  amxc_var_delete(&privileges);
176 }
177 
178 void test_caps_use_user_and_group_id(UNUSED void** state) {
179  amxc_var_t* config = amxrt_get_config();
180  amxc_var_t* privileges = amxc_var_add_key(amxc_htable_t, config, "privileges", NULL);
181 
182  amxc_var_add_key(uint32_t, privileges, "user", 100);
183  amxc_var_add_key(uint32_t, privileges, "group", 100);
184 
185  assert_int_equal(amxrt_caps_apply(), 0);
186 
187  amxc_var_delete(&privileges);
188 }
189 
190 void test_caps_can_keep_capabilities(UNUSED void** state) {
191  amxc_var_t* config = amxrt_get_config();
192  amxc_var_t* privileges = amxc_var_add_key(amxc_htable_t, config, "privileges", NULL);
193  amxc_var_t* capabilities = amxc_var_add_key(amxc_llist_t, privileges, "capabilities", NULL);
194 
195  amxc_var_add(cstring_t, capabilities, "CAP_CHOWN");
196  amxc_var_add(cstring_t, capabilities, "CAP_KILL");
197 
198  will_return_always(__wrap_capng_update, 0);
199  will_return_always(__wrap_capng_apply, 0);
200 
201  assert_int_equal(amxrt_caps_apply(), 0);
202 
203  amxc_var_set_type(capabilities, AMXC_VAR_ID_LIST);
204  amxc_var_add(cstring_t, capabilities, "chown");
205  amxc_var_add(cstring_t, capabilities, "KILL");
206 
207  assert_int_equal(amxrt_caps_apply(), 0);
208 
209  amxc_var_delete(&privileges);
210 }
211 
213  amxc_var_t* config = amxrt_get_config();
214  amxc_var_t* privileges = amxc_var_add_key(amxc_htable_t, config, "privileges", NULL);
215  amxc_var_t* capabilities = amxc_var_add_key(amxc_llist_t, privileges, "capabilities", NULL);
216 
217  amxc_var_add(cstring_t, capabilities, "CAP_DUMMY");
218 
219  amxc_var_set_type(capabilities, AMXC_VAR_ID_LIST);
220  amxc_var_add(cstring_t, capabilities, "dummy");
221 
222  will_return_always(__wrap_capng_apply, 0);
223 
224  assert_int_equal(amxrt_caps_apply(), 0);
225 
226  amxc_var_delete(&privileges);
227 }
228 
229 void test_caps_can_use_capability_ids(UNUSED void** state) {
230  amxc_var_t* config = amxrt_get_config();
231  amxc_var_t* privileges = amxc_var_add_key(amxc_htable_t, config, "privileges", NULL);
232  amxc_var_t* capabilities = amxc_var_add_key(amxc_llist_t, privileges, "capabilities", NULL);
233 
234  amxc_var_add(uint32_t, capabilities, 0);
235  amxc_var_add(uint32_t, capabilities, 10);
236 
237  will_return_always(__wrap_capng_apply, 0);
238 
239  will_return_always(__wrap_capng_update, 0);
240 
241  assert_int_equal(amxrt_caps_apply(), 0);
242 
243  amxc_var_delete(&privileges);
244 }
245 
247  amxc_var_t* config = amxrt_get_config();
248  amxc_var_t* privileges = amxc_var_add_key(amxc_htable_t, config, "privileges", NULL);
249  amxc_var_t* capabilities = amxc_var_add_key(amxc_llist_t, privileges, "capabilities", NULL);
250 
251  amxc_var_add(int32_t, capabilities, -1);
252  amxc_var_add(int32_t, capabilities, 25000);
253 
254  will_return_always(__wrap_capng_apply, 0);
255 
256  assert_int_equal(amxrt_caps_apply(), 0);
257 
258  amxc_var_delete(&privileges);
259 }
260 
261 void test_caps_update_capability_can_fail(UNUSED void** state) {
262  amxc_var_t* config = amxrt_get_config();
263  amxc_var_t* privileges = amxc_var_add_key(amxc_htable_t, config, "privileges", NULL);
264  amxc_var_t* capabilities = amxc_var_add_key(amxc_llist_t, privileges, "capabilities", NULL);
265 
266  amxc_var_add(cstring_t, capabilities, "CAP_KILL");
267 
268  will_return_always(__wrap_capng_update, -1);
269 
270  will_return_always(__wrap_capng_apply, 0);
271 
272  assert_int_equal(amxrt_caps_apply(), 0);
273 
274  amxc_var_delete(&privileges);
275 }
276 
277 void test_caps_apply_can_fail(UNUSED void** state) {
278  amxc_var_t* config = amxrt_get_config();
279  amxc_var_t* privileges = amxc_var_add_key(amxc_htable_t, config, "privileges", NULL);
280  amxc_var_t* capabilities = amxc_var_add_key(amxc_llist_t, privileges, "capabilities", NULL);
281 
282  amxc_var_add(cstring_t, capabilities, "CAP_CHOWN");
283  amxc_var_add(cstring_t, capabilities, "CAP_KILL");
284 
285  will_return_always(__wrap_capng_update, 0);
286  will_return_always(__wrap_capng_apply, -1);
287 
288  assert_int_not_equal(amxrt_caps_apply(), 0);
289 
290  amxc_var_delete(&privileges);
291 }
292 
293 void test_caps_can_dump_capabilities(UNUSED void** state) {
294  assert_int_equal(__real_capng_get_caps_process(), 0);
295 
296  amxrt_caps_dump();
297 }
void amxrt_caps_dump(void)
Dumps the capabilities of the process.
Definition: amxrt_cap.c:180
int amxrt_caps_apply(void)
Apply the user, group and capabilities as defined in the configuration.
Definition: amxrt_cap.c:101
amxc_var_t * amxrt_get_config(void)
Gets the htable variant containing the configuration options.
Definition: amxrt.c:301
void amxrt_delete(void)
Clean-up ambiorix runtime.
Definition: amxrt.c:378
void amxrt_new(void)
Create the ambiorix runtime.
Definition: amxrt.c:313
int __wrap_capng_get_caps_process(void)
void test_caps_apply_can_fail(UNUSED void **state)
void test_caps_invalid_capability_ids_are_ignored(UNUSED void **state)
void test_caps_can_use_capability_ids(UNUSED void **state)
int __real_capng_get_caps_process(void)
int test_caps_teardown(UNUSED void **state)
void __wrap_capng_clear(capng_select_t set)
void test_caps_use_user_and_group_id(UNUSED void **state)
int __wrap_capng_change_id(int uid, int gid, capng_flags_t flag)
void test_caps_can_dump_capabilities(UNUSED void **state)
int __wrap_capng_update(capng_act_t action, capng_type_t type, unsigned int capability)
void test_caps_update_capability_can_fail(UNUSED void **state)
void test_caps_unknown_capabilities_are_ignored(UNUSED void **state)
void test_caps_switch_user_group(UNUSED void **state)
void test_caps_use_non_existing_user_group(UNUSED void **state)
void test_caps_can_keep_capabilities(UNUSED void **state)
void test_caps_use_null_user_group(UNUSED void **state)
struct passwd * __wrap_getpwnam(const char *__name)
int test_caps_setup(UNUSED void **state)
int __wrap_capng_apply(capng_select_t set)
struct group * __wrap_getgrnam(const char *__name)
config
Definition: test.odl:54