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