Skip to content

Commit e7d718a

Browse files
committed
bus: add code to create custom endpoints and set their policy
Custom endpoints are alternative connection points to a bus, allowing specific policy to be uploaded. Add two functions to bus-kernel. One to create such endpoints, and another one for setting a policy for them.
1 parent bb7dd0b commit e7d718a

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/libsystemd/sd-bus/bus-kernel.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <fcntl.h>
2727
#include <malloc.h>
28+
#include <libgen.h>
2829
#include <sys/mman.h>
2930
#include <sys/prctl.h>
3031

@@ -1408,6 +1409,95 @@ int bus_kernel_open_bus_fd(const char *bus, char **path) {
14081409
return fd;
14091410
}
14101411

1412+
int bus_kernel_create_endpoint(const char *bus_name, const char *ep_name, char **ep_path) {
1413+
_cleanup_free_ char *path;
1414+
struct kdbus_cmd_make *make;
1415+
struct kdbus_item *n;
1416+
size_t size;
1417+
int fd;
1418+
1419+
fd = bus_kernel_open_bus_fd(bus_name, &path);
1420+
if (fd < 0)
1421+
return fd;
1422+
1423+
size = ALIGN8(offsetof(struct kdbus_cmd_make, items));
1424+
size += ALIGN8(offsetof(struct kdbus_item, str) + strlen(ep_name) + 1);
1425+
1426+
make = alloca0(size);
1427+
make->size = size;
1428+
make->flags = KDBUS_MAKE_ACCESS_WORLD;
1429+
1430+
n = make->items;
1431+
1432+
n->type = KDBUS_ITEM_MAKE_NAME;
1433+
n->size = offsetof(struct kdbus_item, str) + strlen(ep_name) + 1;
1434+
strcpy(n->str, ep_name);
1435+
1436+
if (ioctl(fd, KDBUS_CMD_EP_MAKE, make) < 0) {
1437+
safe_close(fd);
1438+
return -errno;
1439+
}
1440+
1441+
/* The higher 32bit of the flags field are considered
1442+
* 'incompatible flags'. Refuse them all for now. */
1443+
if (make->flags > 0xFFFFFFFFULL) {
1444+
safe_close(fd);
1445+
return -ENOTSUP;
1446+
}
1447+
1448+
if (ep_path) {
1449+
asprintf(ep_path, "%s/%s", dirname(path), ep_name);
1450+
if (!*ep_path)
1451+
return -ENOMEM;
1452+
}
1453+
1454+
return fd;
1455+
}
1456+
1457+
int bus_kernel_set_endpoint_policy(int fd, uid_t uid, BusEndpoint *ep) {
1458+
1459+
struct kdbus_cmd_update *update;
1460+
struct kdbus_item *n;
1461+
BusEndpointPolicy *po;
1462+
Iterator i;
1463+
size_t size;
1464+
int r;
1465+
1466+
size = ALIGN8(offsetof(struct kdbus_cmd_update, items));
1467+
1468+
HASHMAP_FOREACH(po, ep->policy_hash, i) {
1469+
size += ALIGN8(offsetof(struct kdbus_item, str) + strlen(po->name) + 1);
1470+
size += ALIGN8(offsetof(struct kdbus_item, policy_access) + sizeof(struct kdbus_policy_access));
1471+
}
1472+
1473+
update = alloca0(size);
1474+
update->size = size;
1475+
1476+
n = update->items;
1477+
1478+
HASHMAP_FOREACH(po, ep->policy_hash, i) {
1479+
n->type = KDBUS_ITEM_NAME;
1480+
n->size = offsetof(struct kdbus_item, str) + strlen(po->name) + 1;
1481+
strcpy(n->str, po->name);
1482+
n = KDBUS_ITEM_NEXT(n);
1483+
1484+
n->type = KDBUS_ITEM_POLICY_ACCESS;
1485+
n->size = offsetof(struct kdbus_item, policy_access) + sizeof(struct kdbus_policy_access);
1486+
1487+
n->policy_access.type = KDBUS_POLICY_ACCESS_USER;
1488+
n->policy_access.access = bus_kernel_translate_access(po->access);
1489+
n->policy_access.id = uid;
1490+
1491+
n = KDBUS_ITEM_NEXT(n);
1492+
}
1493+
1494+
r = ioctl(fd, KDBUS_CMD_EP_UPDATE, update);
1495+
if (r < 0)
1496+
return -errno;
1497+
1498+
return 0;
1499+
}
1500+
14111501
int bus_kernel_make_starter(
14121502
int fd,
14131503
const char *name,

src/libsystemd/sd-bus/bus-kernel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <stdbool.h>
2525

2626
#include "busname.h"
27+
#include "bus-endpoint.h"
2728
#include "sd-bus.h"
2829

2930
#define KDBUS_ITEM_NEXT(item) \
@@ -69,8 +70,11 @@ int bus_kernel_open_bus_fd(const char *bus, char **path);
6970
int bus_kernel_make_starter(int fd, const char *name, bool activating, bool accept_fd, BusNamePolicy *policy, BusPolicyAccess world_policy);
7071

7172
int bus_kernel_create_bus(const char *name, bool world, char **s);
73+
int bus_kernel_create_endpoint(const char *bus_name, const char *ep_name, char **path);
7274
int bus_kernel_create_domain(const char *name, char **s);
7375

76+
int bus_kernel_set_endpoint_policy(int fd, uid_t uid, BusEndpoint *ep);
77+
7478
int bus_kernel_pop_memfd(sd_bus *bus, void **address, size_t *mapped, size_t *allocated);
7579
void bus_kernel_push_memfd(sd_bus *bus, int fd, void *address, size_t mapped, size_t allocated);
7680

0 commit comments

Comments
 (0)