usb: gadget: f_fs: Prevent panic due to failure of huge size buffer allocation
[platform/kernel/linux-rpi.git] / tools / testing / selftests / kdbus / test-policy-ns.c
1 /*
2  * Test metadata and policies in new namespaces. Even if our tests
3  * can run in a namespaced setup, this test is necessary so we can
4  * inspect policies on the same kdbusfs but between multiple
5  * namespaces.
6  *
7  * Copyright (C) 2014-2015 Djalal Harouni
8  *
9  * kdbus is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License as published by the
11  * Free Software Foundation; either version 2.1 of the License, or (at
12  * your option) any later version.
13  */
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #include <pthread.h>
19 #include <sched.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <signal.h>
27 #include <sys/wait.h>
28 #include <sys/prctl.h>
29 #include <sys/eventfd.h>
30 #include <sys/syscall.h>
31 #include <sys/capability.h>
32 #include <linux/sched.h>
33
34 #include "kdbus-test.h"
35 #include "kdbus-util.h"
36 #include "kdbus-enum.h"
37
38 #define MAX_CONN        64
39 #define POLICY_NAME     "foo.test.policy-test"
40
41 #define KDBUS_CONN_MAX_MSGS_PER_USER            16
42
43 /**
44  * Note: this test can be used to inspect policy_db->talk_access_hash
45  *
46  * The purpose of these tests:
47  * 1) Check KDBUS_POLICY_TALK
48  * 2) Check the cache state: kdbus_policy_db->talk_access_hash
49  * Should be extended
50  */
51
52 /**
53  * Check a list of connections against conn_db[0]
54  * conn_db[0] will own the name "foo.test.policy-test" and the
55  * policy holder connection for this name will update the policy
56  * entries, so different use cases can be tested.
57  */
58 static struct kdbus_conn **conn_db;
59
60 static wur void *kdbus_recv_echo(void *ptr)
61 {
62         int ret;
63         struct kdbus_conn *conn = ptr;
64
65         ret = kdbus_msg_recv_poll(conn, 3009, NULL, NULL);
66
67         return (void *)(long)ret;
68 }
69
70 /* Trigger kdbus_policy_set() */
71 static wur int kdbus_set_policy_talk(struct kdbus_conn *conn,
72                                  const char *name,
73                                  uid_t id, unsigned int type)
74 {
75         struct kdbus_policy_access access = {
76                 .type = type,
77                 .id = id,
78                 .access = KDBUS_POLICY_TALK,
79         };
80
81         ASSERT_ZERO(kdbus_conn_update_policy(conn, name, &access, 1));
82
83         return TEST_OK;
84 }
85
86 /* return TEST_OK or TEST_ERR on failure */
87 static wur int kdbus_register_same_activator(char *bus, const char *name,
88                                          struct kdbus_conn **c)
89 {
90         int ret;
91         struct kdbus_conn *activator;
92
93         activator = kdbus_hello_activator(bus, name, NULL, 0);
94         if (activator) {
95                 *c = activator;
96                 print("--- error was able to register name twice '%s'.\n", name);
97                 return TEST_ERR;
98         }
99
100         ret = -errno;
101         /* -EEXIST means test succeeded */
102         if (ret == -EEXIST)
103                 return TEST_OK;
104
105         return TEST_ERR;
106 }
107
108 /* return TEST_OK or TEST_ERR on failure */
109 static wur int kdbus_register_policy_holder(char *bus, const char *name,
110                                         struct kdbus_conn **conn)
111 {
112         struct kdbus_conn *c;
113         struct kdbus_policy_access access[2];
114
115         access[0].type = KDBUS_POLICY_ACCESS_USER;
116         access[0].access = KDBUS_POLICY_OWN;
117         access[0].id = geteuid();
118
119         access[1].type = KDBUS_POLICY_ACCESS_WORLD;
120         access[1].access = KDBUS_POLICY_TALK;
121         access[1].id = geteuid();
122
123         ASSERT_NONZERO(c = kdbus_hello_registrar(bus, name, access, 2, KDBUS_HELLO_POLICY_HOLDER));
124
125         *conn = c;
126
127         return TEST_OK;
128 }
129
130 /**
131  * Create new threads for receiving from multiple senders,
132  * The 'conn_db' will be populated by newly created connections.
133  * Caller should free all allocated connections.
134  *
135  * return 0 on success, negative errno on failure.
136  */
137 static wur int kdbus_recv_in_threads(const char *bus, const char *name,
138                                  struct kdbus_conn **conn_db)
139 {
140         int ret;
141         bool pool_full = false;
142         unsigned int sent_packets = 0;
143         unsigned int lost_packets = 0;
144         unsigned int i, tid;
145         unsigned long dst_id;
146         unsigned long cookie = 1;
147         unsigned int thread_nr = MAX_CONN - 1;
148         pthread_t thread_id[MAX_CONN - 1] = {'\0'};
149
150         dst_id = name ? KDBUS_DST_ID_NAME : conn_db[0]->id;
151
152         for (tid = 0, i = 1; tid < thread_nr; tid++, i++) {
153                 ret = pthread_create(&thread_id[tid], NULL,
154                                      kdbus_recv_echo, (void *)conn_db[0]);
155                 if (ret < 0) {
156                         ret = -errno;
157                         kdbus_printf("error pthread_create: %d (%m)\n",
158                                       ret);
159                         break;
160                 }
161
162                 /* just free before re-using */
163                 kdbus_conn_free(conn_db[i]);
164                 conn_db[i] = NULL;
165
166                 /* We need to create connections here */
167                 conn_db[i] = kdbus_hello(bus, 0, NULL, 0);
168                 if (!conn_db[i]) {
169                         ret = -errno;
170                         break;
171                 }
172
173                 ret = kdbus_add_match_empty(conn_db[i]);
174                 if (ret < 0)
175                         break;
176
177                 ret = kdbus_msg_send(conn_db[i], name, cookie++,
178                                      0, 0, 0, dst_id);
179                 if (ret < 0) {
180                         /*
181                          * Receivers are not reading their messages,
182                          * not scheduled ?!
183                          *
184                          * So set the pool full here, perhaps the
185                          * connection pool or queue was full, later
186                          * recheck receivers errors
187                          */
188                         if (ret == -ENOBUFS || ret == -EXFULL)
189                                 pool_full = true;
190                         break;
191                 }
192
193                 sent_packets++;
194         }
195
196         for (tid = 0; tid < thread_nr; tid++) {
197                 int thread_ret = 0;
198
199                 if (thread_id[tid]) {
200                         pthread_join(thread_id[tid], (void *)&thread_ret);
201                         if (thread_ret < 0) {
202                                 /* Update only if send did not fail */
203                                 if (ret == 0)
204                                         ret = thread_ret;
205
206                                 lost_packets++;
207                         }
208                 }
209         }
210
211         /*
212          * When sending if we did fail with -ENOBUFS or -EXFULL
213          * then we should have set lost_packet and we should at
214          * least have sent_packets set to KDBUS_CONN_MAX_MSGS_PER_USER
215          */
216         if (pool_full) {
217                 ASSERT_NONZERO(lost_packets);
218
219                 /*
220                  * We should at least send KDBUS_CONN_MAX_MSGS_PER_USER
221                  *
222                  * For every send operation we create a thread to
223                  * recv the packet, so we keep the queue clean
224                  */
225                 ASSERT_RETURN(sent_packets,>=,(unsigned)KDBUS_CONN_MAX_MSGS_PER_USER);
226
227                 /*
228                  * Set ret to zero since we only failed due to
229                  * the receiving threads that have not been
230                  * scheduled
231                  */
232                 ret = 0;
233         }
234
235         return ret;
236 }
237
238 /* Return: TEST_OK or TEST_ERR on failure */
239 static wur int kdbus_normal_test(const char *bus, const char *name,
240                              struct kdbus_conn **conn_db)
241 {
242         ASSERT_RETURN(0,<=,kdbus_recv_in_threads(bus, name, conn_db));
243         return TEST_OK;
244 }
245
246 static wur int kdbus_fork_test_by_id(const char *bus,
247                                  struct kdbus_conn **conn_db,
248                                  int parent_status, int child_status)
249 {
250         int ret;
251         pid_t pid;
252         uint64_t cookie = 0x9876ecba;
253         struct kdbus_msg *msg = NULL;
254         uint64_t offset = 0;
255         int status = 0;
256
257         bool parent_timedout;
258         bool child_timedout;
259
260 #ifdef TIZEN
261         child_status = parent_status = 0;
262 #endif
263
264         /*
265          * If the child_status is not EXIT_SUCCESS, then we expect
266          * that sending from the child will fail, thus receiving
267          * from parent must error with -ETIMEDOUT, and vice versa.
268          */
269         parent_timedout = !!child_status;
270         child_timedout = !!parent_status;
271
272         pid = fork();
273         ASSERT_RETURN_VAL(pid,>=,0, pid);
274
275         if (pid == 0) {
276                 struct kdbus_conn *conn_src;
277
278                 ASSERT_EXIT_ZERO(prctl(PR_SET_PDEATHSIG, SIGKILL));
279
280                 ASSERT_EXIT_ZERO(drop_privileges(65534, 65534));
281
282                 ASSERT_EXIT_NONZERO(conn_src = kdbus_hello(bus, 0, NULL, 0));
283
284                 ASSERT_EXIT_ZERO(kdbus_add_match_empty(conn_src));
285
286                 /*
287                  * child_status is always checked against send
288                  * operations, in case it fails always return
289                  * EXIT_FAILURE.
290                  */
291                 ASSERT_EXIT(child_status,==,kdbus_msg_send(conn_src, NULL, cookie, 0, 0, 0, conn_db[0]->id));
292
293                 ret = kdbus_msg_recv_poll(conn_src, 1009, NULL, NULL);
294
295                 kdbus_conn_free(conn_src);
296
297                 /*
298                  * Child kdbus_msg_recv_poll() should timeout since
299                  * the parent_status was set to a non EXIT_SUCCESS
300                  * value.
301                  */
302                 if (child_timedout)
303                         exit(ret == -ETIMEDOUT ? EXIT_SUCCESS : EXIT_FAILURE);
304
305                 exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
306         }
307
308         ret = kdbus_msg_recv_poll(conn_db[0], 1009, &msg, &offset);
309         /*
310          * If parent_timedout is set then this should fail with
311          * -ETIMEDOUT since the child_status was set to a non
312          * EXIT_SUCCESS value. Otherwise, assume
313          * that kdbus_msg_recv_poll() has succeeded.
314          */
315         if (parent_timedout) {
316                 ASSERT_RETURN_VAL(ret,==,-ETIMEDOUT, TEST_ERR);
317
318                 /* timedout no need to continue, we don't have the
319                  * child connection ID, so just terminate. */
320                 goto out;
321         } else
322                 ASSERT_RETURN_VAL(ret,==,0, ret);
323
324         ret = kdbus_msg_send(conn_db[0], NULL, ++cookie, 0, 0, 0, msg->src_id);
325         /*
326          * parent_status is checked against send operations,
327          * on failures always return TEST_ERR.
328          */
329         ASSERT_RETURN_VAL(ret,==,parent_status, TEST_ERR);
330
331         kdbus_msg_free(msg);
332         ASSERT_ZERO(kdbus_free(conn_db[0], offset));
333
334 out:
335         ret = waitpid(pid, &status, 0);
336         ASSERT_RETURN_VAL(ret,>=,0, ret);
337
338         return (status == EXIT_SUCCESS) ? TEST_OK : TEST_ERR;
339 }
340
341 /*
342  * Return: TEST_OK, TEST_ERR or TEST_SKIP
343  * we return TEST_OK only if the children return with the expected
344  * 'expected_status' that is specified as an argument.
345  */
346 static wur int kdbus_fork_test(const char *bus, const char *name,
347                            struct kdbus_conn **conn_db, int expected_status)
348 {
349         pid_t pid;
350         int ret = 0;
351         int status = 0;
352
353         pid = fork();
354         ASSERT_RETURN_VAL(pid,>=,0, pid);
355
356         if (pid == 0) {
357                 ASSERT_EXIT_ZERO(prctl(PR_SET_PDEATHSIG, SIGKILL));
358
359                 ASSERT_EXIT_ZERO(drop_privileges(65534, 65534));
360
361                 ret = kdbus_recv_in_threads(bus, name, conn_db);
362                 exit(ret == expected_status ? EXIT_SUCCESS : EXIT_FAILURE);
363         }
364
365         ASSERT_RETURN(0,<=,waitpid(pid, &status, 0));
366
367         return (status == EXIT_SUCCESS) ? TEST_OK : TEST_ERR;
368 }
369
370 /* Return EXIT_SUCCESS, EXIT_FAILURE or negative errno */
371 static wur int __kdbus_clone_userns_test(const char *bus,
372                                      const char *name,
373                                      int expected_status)
374 {
375         int efd;
376         pid_t pid;
377         int ret = 0;
378         unsigned int uid = 65534;
379         int status;
380
381         ret = drop_privileges(uid, uid);
382         ASSERT_RETURN_VAL(ret,==,0, ret);
383
384         /*
385          * Since we just dropped privileges, the dumpable flag was just
386          * cleared which makes the /proc/$clone_child/uid_map to be
387          * owned by root, hence any userns uid mapping will fail with
388          * -EPERM since the mapping will be done by uid 65534.
389          *
390          * To avoid this set the dumpable flag again which makes procfs
391          * update the /proc/$clone_child/ inodes owner to 65534.
392          *
393          * Using this we will be able write to /proc/$clone_child/uid_map
394          * as uid 65534 and map the uid 65534 to 0 inside the user
395          * namespace.
396          */
397         ret = prctl(PR_SET_DUMPABLE, SUID_DUMP_USER);
398         ASSERT_RETURN_VAL(ret,==,0, ret);
399
400         /* sync parent/child */
401         efd = eventfd(0, EFD_CLOEXEC);
402         ASSERT_RETURN_VAL(efd,>=,0, efd);
403
404         pid = syscall(__NR_clone, SIGCHLD|CLONE_NEWUSER, NULL);
405         if (pid < 0) {
406                 ret = -errno;
407                 kdbus_printf("error clone: %d (%m)\n", ret);
408                 /*
409                  * Normal user not allowed to create userns,
410                  * so nothing to worry about ?
411                  */
412                 if (ret == -EPERM) {
413                         kdbus_printf("-- CLONE_NEWUSER TEST Failed for uid: %u\n"
414                                 "-- Make sure that your kernel do not allow "
415                                 "CLONE_NEWUSER for unprivileged users\n"
416                                 "-- Upstream Commit: "
417                                 "https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=5eaf563e\n",
418                                 uid);
419                         ret = 0;
420                 }
421
422                 return ret;
423         }
424
425         if (pid == 0) {
426                 struct kdbus_conn *conn_src;
427                 eventfd_t event_status = 0;
428
429                 ASSERT_EXIT_ZERO(prctl(PR_SET_PDEATHSIG, SIGKILL));
430
431                 ASSERT_EXIT(0,<=,eventfd_read(efd, &event_status));
432                 ASSERT_EXIT(event_status,==,(eventfd_t)1);
433
434                 /* ping connection from the new user namespace */
435                 ASSERT_EXIT_NONZERO(conn_src = kdbus_hello(bus, 0, NULL, 0));
436
437                 ASSERT_EXIT_ZERO(kdbus_add_match_empty(conn_src));
438
439                 ret = kdbus_msg_send(conn_src, name, 0xabcd1234, 0, 0, 0, KDBUS_DST_ID_NAME);
440                 kdbus_conn_free(conn_src);
441
442                 exit(ret == expected_status ? EXIT_SUCCESS : EXIT_FAILURE);
443         }
444
445         ret = userns_map_uid_gid(pid, "0 65534 1", "0 65534 1");
446         ASSERT_RETURN_VAL(ret,==,0, ret);
447
448         /* Tell child we are ready */
449         ret = eventfd_write(efd, 1);
450         ASSERT_RETURN_VAL(ret,==,0, ret);
451
452         ret = waitpid(pid, &status, 0);
453         ASSERT_RETURN_VAL(ret,>=,0, ret);
454
455         CLOSE(efd);
456
457         return status == EXIT_SUCCESS ? TEST_OK : TEST_ERR;
458 }
459
460 static wur int kdbus_clone_userns_test(const char *bus,
461                                    const char *name,
462                                    struct kdbus_conn **conn_db,
463                                    int expected_status)
464 {
465         pid_t pid;
466         int ret = 0;
467         int status;
468
469         pid = fork();
470         ASSERT_RETURN_VAL(pid,>=,0, -errno);
471
472         if (pid == 0) {
473                 ret = prctl(PR_SET_PDEATHSIG, SIGKILL);
474                 if (ret < 0)
475                         exit(EXIT_FAILURE);
476
477                 exit(__kdbus_clone_userns_test(bus, name, expected_status));
478         }
479
480         /*
481          * Receive in the original (root privileged) user namespace,
482          * must fail with -ETIMEDOUT.
483          */
484         ret = kdbus_msg_recv_poll(conn_db[0], 1009, NULL, NULL);
485         ASSERT_RETURN(ret,==,expected_status?-ETIMEDOUT:0);
486
487         ret = waitpid(pid, &status, 0);
488         ASSERT_RETURN(ret,>=,0);
489
490         return (status == EXIT_SUCCESS) ? TEST_OK : TEST_ERR;
491 }
492
493 wur int kdbus_test_policy_ns(struct kdbus_test_env *env)
494 {
495         int i;
496         int ret;
497         struct kdbus_conn *activator = NULL;
498         struct kdbus_conn *policy_holder = NULL;
499         char *bus = env->buspath;
500
501         ret = test_is_capable(CAP_SETUID, CAP_SETGID, -1);
502         ASSERT_RETURN(ret,>=,0);
503
504         /* no enough privileges, SKIP test */
505         if (!ret)
506                 return TEST_SKIP;
507
508         /* we require user-namespaces */
509         if (access("/proc/self/uid_map", F_OK) != 0)
510                 return TEST_SKIP;
511
512         /* uids/gids must be mapped */
513         if (!all_uids_gids_are_mapped())
514                 return TEST_SKIP;
515
516         ASSERT_NONZERO(conn_db = calloc(MAX_CONN, sizeof(struct kdbus_conn *)));
517
518         memset(conn_db, 0, MAX_CONN * sizeof(struct kdbus_conn *));
519
520         ASSERT_NONZERO(conn_db[0] = kdbus_hello(bus, 0, NULL, 0));
521
522         ASSERT_ZERO(kdbus_add_match_empty(conn_db[0]));
523
524         ASSERT_EXIT_ZERO(kdbus_fork_test_by_id(bus, conn_db, -EPERM, -EPERM));
525
526         ASSERT_ZERO(kdbus_register_policy_holder(bus, POLICY_NAME, &policy_holder));
527
528         /* Try to register the same name with an activator */
529         ASSERT_ZERO(kdbus_register_same_activator(bus, POLICY_NAME, &activator));
530
531         /* Acquire POLICY_NAME */
532         ASSERT_ZERO(kdbus_name_acquire(conn_db[0], POLICY_NAME, NULL));
533
534         ASSERT_ZERO(kdbus_normal_test(bus, POLICY_NAME, conn_db));
535
536         ASSERT_ZERO(kdbus_list(conn_db[0], KDBUS_LIST_NAMES | KDBUS_LIST_UNIQUE | KDBUS_LIST_ACTIVATORS | KDBUS_LIST_QUEUED));
537
538         ASSERT_ZERO(kdbus_fork_test(bus, POLICY_NAME, conn_db, EXIT_SUCCESS));
539
540         /*
541          * children connections are able to talk to conn_db[0] since
542          * current POLICY_NAME TALK type is KDBUS_POLICY_ACCESS_WORLD,
543          * so expect EXIT_SUCCESS when sending from child. However,
544          * since the child's connection does not own any well-known
545          * name, The parent connection conn_db[0] should fail with
546          * -EPERM but since it is a privileged bus user the TALK is
547          *  allowed.
548          */
549         ASSERT_EXIT_ZERO(kdbus_fork_test_by_id(bus, conn_db, EXIT_SUCCESS, EXIT_SUCCESS));
550
551         /*
552          * Connections that can talk are perhaps being destroyed now.
553          * Restrict the policy and purge cache entries where the
554          * conn_db[0] is the destination.
555          *
556          * Now only connections with uid == 0 are allowed to talk.
557          */
558         ASSERT_ZERO(kdbus_set_policy_talk(policy_holder, POLICY_NAME, geteuid(), KDBUS_POLICY_ACCESS_USER));
559
560         /*
561          * Testing connections (FORK+DROP) again:
562          * After setting the policy re-check connections
563          * we expect the children to fail with -EPERM
564          */
565         ASSERT_ZERO(kdbus_fork_test(bus, POLICY_NAME, conn_db, ONTIZEN(0,-EPERM)));
566
567         /*
568          * Now expect that both parent and child to fail.
569          *
570          * Child should fail with -EPERM since we just restricted
571          * the POLICY_NAME TALK to uid 0 and its uid is 65534.
572          *
573          * Since the parent's connection will timeout when receiving
574          * from the child, we never continue. FWIW just put -EPERM.
575          */
576         ASSERT_EXIT_ZERO(kdbus_fork_test_by_id(bus, conn_db, -EPERM, -EPERM));
577
578         /* Check if the name can be reached in a new userns */
579         ASSERT_ZERO(kdbus_clone_userns_test(bus, POLICY_NAME, conn_db, ONTIZEN(0,-EPERM)));
580
581         for (i = 0; i < MAX_CONN; i++)
582                 kdbus_conn_free(conn_db[i]);
583
584         kdbus_conn_free(activator);
585         kdbus_conn_free(policy_holder);
586
587         free(conn_db);
588
589         return TEST_OK;
590 }