kdbus: the driver, original and non-working
[platform/kernel/linux-exynos.git] / tools / testing / selftests / kdbus / kdbus-util.h
1 /*
2  * Copyright (C) 2013-2015 Kay Sievers
3  * Copyright (C) 2013-2015 Daniel Mack
4  *
5  * kdbus is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at
8  * your option) any later version.
9  */
10 #pragma once
11
12 #define BIT(X) (1 << (X))
13
14 #include <time.h>
15 #include <stdbool.h>
16 #include <linux/kdbus.h>
17
18 #define _STRINGIFY(x) #x
19 #define STRINGIFY(x) _STRINGIFY(x)
20 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
21
22 #define KDBUS_PTR(addr) ((void *)(uintptr_t)(addr))
23
24 #define KDBUS_ALIGN8(l) (((l) + 7) & ~7)
25 #define KDBUS_ITEM_HEADER_SIZE offsetof(struct kdbus_item, data)
26 #define KDBUS_ITEM_SIZE(s) KDBUS_ALIGN8((s) + KDBUS_ITEM_HEADER_SIZE)
27
28 #define KDBUS_ITEM_NEXT(item) \
29         (typeof(item))(((uint8_t *)item) + KDBUS_ALIGN8((item)->size))
30 #define KDBUS_ITEM_FOREACH(item, head, first)                           \
31         for (item = (head)->first;                                      \
32              ((uint8_t *)(item) < (uint8_t *)(head) + (head)->size) &&  \
33                ((uint8_t *)(item) >= (uint8_t *)(head));        \
34              item = KDBUS_ITEM_NEXT(item))
35 #define KDBUS_FOREACH(iter, first, _size)                               \
36         for (iter = (first);                                            \
37              ((uint8_t *)(iter) < (uint8_t *)(first) + (_size)) &&      \
38                ((uint8_t *)(iter) >= (uint8_t *)(first));               \
39              iter = (void*)(((uint8_t *)iter) + KDBUS_ALIGN8((iter)->size)))
40
41
42 #define _KDBUS_ATTACH_BITS_SET_NR  (__builtin_popcountll(_KDBUS_ATTACH_ALL))
43
44 /* Sum of KDBUS_ITEM_* that reflects _KDBUS_ATTACH_ALL */
45 #define KDBUS_ATTACH_ITEMS_TYPE_SUM \
46         ((((_KDBUS_ATTACH_BITS_SET_NR - 1) * \
47         ((_KDBUS_ATTACH_BITS_SET_NR - 1) + 1)) / 2 ) + \
48         (_KDBUS_ITEM_ATTACH_BASE * _KDBUS_ATTACH_BITS_SET_NR))
49
50
51 #define POOL_SIZE (16 * 1024LU * 1024LU)
52
53 #define UNPRIV_UID 65534
54 #define UNPRIV_GID 65534
55
56 /* Dump as user of process, useful for user namespace testing */
57 #define SUID_DUMP_USER  1
58
59 extern int kdbus_util_verbose;
60
61 #define kdbus_printf(X...) \
62         if (kdbus_util_verbose) \
63                 printf(X)
64
65 #define RUN_UNPRIVILEGED(child_uid, child_gid, _child_, _parent_) ({    \
66                 pid_t pid, rpid;                                        \
67                 int ret;                                                \
68                                                                         \
69                 pid = fork();                                           \
70                 if (pid == 0) {                                         \
71                         ret = drop_privileges(child_uid, child_gid);    \
72                         ASSERT_EXIT_VAL(ret == 0, ret);                 \
73                                                                         \
74                         _child_;                                        \
75                         _exit(0);                                       \
76                 } else if (pid > 0) {                                   \
77                         _parent_;                                       \
78                         rpid = waitpid(pid, &ret, 0);                   \
79                         ASSERT_RETURN(rpid == pid);                     \
80                         ASSERT_RETURN(WIFEXITED(ret));                  \
81                         ASSERT_RETURN(WEXITSTATUS(ret) == 0);           \
82                         ret = TEST_OK;                                  \
83                 } else {                                                \
84                         ret = pid;                                      \
85                 }                                                       \
86                                                                         \
87                 ret;                                                    \
88         })
89
90 #define RUN_UNPRIVILEGED_CONN(_var_, _bus_, _code_)                     \
91         RUN_UNPRIVILEGED(UNPRIV_UID, UNPRIV_GID, ({                     \
92                 struct kdbus_conn *_var_;                               \
93                 _var_ = kdbus_hello(_bus_, 0, NULL, 0);                 \
94                 ASSERT_EXIT(_var_);                                     \
95                 _code_;                                                 \
96                 kdbus_conn_free(_var_);                                 \
97         }), ({ 0; }))
98
99 #define RUN_CLONE_CHILD(clone_ret, flags, _setup_, _child_body_,        \
100                         _parent_setup_, _parent_body_) ({               \
101         pid_t pid, rpid;                                                \
102         int ret;                                                        \
103         int efd = -1;                                                   \
104                                                                         \
105         _setup_;                                                        \
106         efd = eventfd(0, EFD_CLOEXEC);                                  \
107         ASSERT_RETURN(efd >= 0);                                        \
108         *clone_ret = 0;                                                 \
109         pid = syscall(__NR_clone, flags, NULL);                         \
110         if (pid == 0) {                                                 \
111                 eventfd_t event_status = 0;                             \
112                 ret = prctl(PR_SET_PDEATHSIG, SIGKILL);                 \
113                 ASSERT_EXIT(ret == 0);                                  \
114                 ret = eventfd_read(efd, &event_status);                 \
115                 if (ret < 0 || event_status != 1) {                     \
116                         kdbus_printf("error eventfd_read()\n");         \
117                         _exit(EXIT_FAILURE);                            \
118                 }                                                       \
119                 _child_body_;                                           \
120                 _exit(0);                                               \
121         } else if (pid > 0) {                                           \
122                 _parent_setup_;                                         \
123                 ret = eventfd_write(efd, 1);                            \
124                 ASSERT_RETURN(ret >= 0);                                \
125                 _parent_body_;                                          \
126                 rpid = waitpid(pid, &ret, 0);                           \
127                 ASSERT_RETURN(rpid == pid);                             \
128                 ASSERT_RETURN(WIFEXITED(ret));                          \
129                 ASSERT_RETURN(WEXITSTATUS(ret) == 0);                   \
130                 ret = TEST_OK;                                          \
131         } else {                                                        \
132                 ret = -errno;                                           \
133                 *clone_ret = -errno;                                    \
134         }                                                               \
135         close(efd);                                                     \
136         ret;                                                            \
137 })
138
139 /* Enums for parent if it should drop privs or not */
140 enum kdbus_drop_parent {
141         DO_NOT_DROP,
142         DROP_SAME_UNPRIV,
143         DROP_OTHER_UNPRIV,
144 };
145
146 struct kdbus_conn {
147         int fd;
148         uint64_t id;
149         unsigned char *buf;
150 };
151
152 int kdbus_sysfs_get_parameter_mask(const char *path, uint64_t *mask);
153 int kdbus_sysfs_set_parameter_mask(const char *path, uint64_t mask);
154
155 int sys_memfd_create(const char *name, __u64 size);
156 int sys_memfd_seal_set(int fd);
157 off_t sys_memfd_get_size(int fd, off_t *size);
158
159 int kdbus_list(struct kdbus_conn *conn, uint64_t flags);
160 int kdbus_name_release(struct kdbus_conn *conn, const char *name);
161 int kdbus_name_acquire(struct kdbus_conn *conn, const char *name,
162                        uint64_t *flags);
163 void kdbus_msg_free(struct kdbus_msg *msg);
164 int kdbus_msg_recv(struct kdbus_conn *conn,
165                    struct kdbus_msg **msg, uint64_t *offset);
166 int kdbus_msg_recv_poll(struct kdbus_conn *conn, int timeout_ms,
167                         struct kdbus_msg **msg_out, uint64_t *offset);
168 int kdbus_free(const struct kdbus_conn *conn, uint64_t offset);
169 int kdbus_msg_dump(const struct kdbus_conn *conn,
170                    const struct kdbus_msg *msg);
171 int kdbus_create_bus(int control_fd, const char *name,
172                      uint64_t req_meta, uint64_t owner_meta,
173                      char **path);
174 int kdbus_msg_send(const struct kdbus_conn *conn, const char *name,
175                    uint64_t cookie, uint64_t flags, uint64_t timeout,
176                    int64_t priority, uint64_t dst_id, int fds_count, int fds[]);
177 int kdbus_msg_send_sync(const struct kdbus_conn *conn, const char *name,
178                         uint64_t cookie, uint64_t flags, uint64_t timeout,
179                         int64_t priority, uint64_t dst_id, int cancel_fd);
180 int kdbus_msg_send_reply(const struct kdbus_conn *conn,
181                          uint64_t reply_cookie,
182                          uint64_t dst_id);
183 struct kdbus_conn *kdbus_hello(const char *path, uint64_t hello_flags,
184                                const struct kdbus_item *item,
185                                size_t item_size);
186 struct kdbus_conn *kdbus_hello_registrar(const char *path, const char *name,
187                                          const struct kdbus_policy_access *access,
188                                          size_t num_access, uint64_t flags);
189 struct kdbus_conn *kdbus_hello_activator(const char *path, const char *name,
190                                          const struct kdbus_policy_access *access,
191                                          size_t num_access);
192 bool kdbus_item_in_message(struct kdbus_msg *msg, uint64_t type);
193 int kdbus_bus_creator_info(struct kdbus_conn *conn,
194                            uint64_t flags,
195                            uint64_t *offset);
196 int kdbus_conn_info(struct kdbus_conn *conn, uint64_t id,
197                     const char *name, uint64_t flags, uint64_t *offset);
198 void kdbus_conn_free(struct kdbus_conn *conn);
199 int kdbus_conn_update_attach_flags(struct kdbus_conn *conn,
200                                    uint64_t attach_flags_send,
201                                    uint64_t attach_flags_recv);
202 int kdbus_conn_update_policy(struct kdbus_conn *conn, const char *name,
203                              const struct kdbus_policy_access *access,
204                              size_t num_access);
205
206 int kdbus_add_match_id(struct kdbus_conn *conn, uint64_t cookie,
207                        uint64_t type, uint64_t id);
208 int kdbus_add_match_empty(struct kdbus_conn *conn);
209
210 int all_uids_gids_are_mapped();
211 int drop_privileges(uid_t uid, gid_t gid);
212 uint64_t now(clockid_t clock);
213 char *unique_name(const char *prefix);
214
215 int userns_map_uid_gid(pid_t pid,
216                        const char *map_uid,
217                        const char *map_gid);
218 int test_is_capable(int cap, ...);
219 int config_user_ns_is_enabled(void);
220 int config_auditsyscall_is_enabled(void);
221 int config_cgroups_is_enabled(void);
222 int config_security_is_enabled(void);