Skip to content

Commit e1b5983

Browse files
committed
Merge branch 'noroot' of github.com:SafetyInObscurity/mctp
Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
2 parents a174497 + 4e7d706 commit e1b5983

5 files changed

Lines changed: 101 additions & 3 deletions

File tree

conf/mctpd-dbus.conf

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,33 @@
22
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
33
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
44
<busconfig>
5+
<policy context="default">
6+
<deny send_destination="au.com.codeconstruct.MCTP1"/>
7+
8+
<allow receive_sender="au.com.codeconstruct.MCTP1"/>
9+
10+
<allow send_destination="au.com.codeconstruct.MCTP1"
11+
send_interface="org.freedesktop.DBus.Propeties"
12+
send_member="Get"/>
13+
14+
<allow send_destination="au.com.codeconstruct.MCTP1"
15+
send_interface="org.freedesktop.DBus.Propeties"
16+
send_member="GetAll"/>
17+
18+
<allow send_destination="au.com.codeconstruct.MCTP1"
19+
send_interface="org.freedesktop.DBus.Introspectable"/>
20+
21+
<allow send_destination="au.com.codeconstruct.MCTP1"
22+
send_interface="org.freedesktop.DBus.ObjectManager"/>
23+
</policy>
24+
<policy user="mctpd">
25+
<allow own="au.com.codeconstruct.MCTP1"/>
26+
</policy>
27+
<policy group="mctp-admin">
28+
<allow send_destination="au.com.codeconstruct.MCTP1"/>
29+
</policy>
530
<policy user="root">
631
<allow own="au.com.codeconstruct.MCTP1"/>
732
<allow send_destination="au.com.codeconstruct.MCTP1"/>
8-
<allow receive_sender="au.com.codeconstruct.MCTP1"/>
933
</policy>
1034
</busconfig>

conf/mctpd.service

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ After=mctp-local.target
88
Type=dbus
99
BusName=au.com.codeconstruct.MCTP1
1010
ExecStart=/usr/sbin/mctpd
11+
User=mctpd
12+
AmbientCapabilities=CAP_NET_BIND_SERVICE CAP_NET_ADMIN
1113

1214
[Install]
1315
WantedBy=mctp.target

docs/mctpd.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,14 @@ match = { path = "/devices/pci0000:00/0000:00:08.3/*" }
500500
```
501501

502502
Paths have the `/sys` prefix stripped.
503+
504+
## Capabilities
505+
506+
`mctpd` requires the `CAP_NET_BIND_SERVICE` and `CAP_NET_ADMIN` capabilites to run.
507+
If the service is not running as root these should be added to the `AmbientCapabilities`
508+
setting in the service's config.
509+
If the target platform has the `libcap` library available,
510+
the `mctpd` process will drop `CAP_NET_BIND_SERVICE` when it is no longer needed.
511+
512+
Because `mctpd` may be exposed to potentially hostile inputs,
513+
it shouldn't be given more capabilities than necessary.

meson.build

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ add_project_arguments('-Wno-unused-parameter', language : 'c')
1616

1717
libsystemd = dependency('libsystemd', version: '>=247', required: false)
1818

19+
libcap = dependency('libcap', required: false)
20+
1921
conf = configuration_data()
2022
conf.set10('HAVE_LINUX_MCTP_H',
2123
cc.has_header('linux/mctp.h'),
@@ -37,6 +39,10 @@ conf.set10('MCTPD_WRITABLE_CONNECTIVITY',
3739
get_option('unsafe-writable-connectivity'),
3840
description: 'Allow writes to the Connectivity member of the au.com.codeconstruct.MCTP.Endpoint1 interface on endpoint objects')
3941

42+
conf.set10('HAVE_LIBCAP',
43+
libcap.found(),
44+
description: 'Require mctpd to minimise its capabilities')
45+
4046
conf.set_quoted('MCTPD_CONF_FILE_DEFAULT',
4147
join_paths(get_option('prefix'), get_option('sysconfdir'), 'mctpd.conf'),
4248
description: 'Default configuration file path',
@@ -89,7 +95,7 @@ if libsystemd.found()
8995
sources: [
9096
'src/mctpd.c',
9197
] + netlink_sources + util_sources + ops_sources,
92-
dependencies: [libsystemd, toml_dep],
98+
dependencies: [libsystemd, toml_dep, libcap],
9399
install: true,
94100
install_dir: get_option('sbindir'),
95101
c_args: ['-DOPS_SD_EVENT=1'],
@@ -100,7 +106,7 @@ if libsystemd.found()
100106
'src/mctpd.c',
101107
] + test_ops_sources + netlink_sources + util_sources,
102108
include_directories: include_directories('src'),
103-
dependencies: [libsystemd, toml_dep],
109+
dependencies: [libsystemd, toml_dep, libcap],
104110
c_args: ['-DOPS_SD_EVENT=1'],
105111
)
106112
endif

src/mctpd.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#define _GNU_SOURCE
1010
#include "config.h"
1111

12+
#if HAVE_LIBCAP
13+
#include <sys/capability.h>
14+
#endif
15+
1216
#include <assert.h>
1317
#include <systemd/sd-bus-vtable.h>
1418
#include <time.h>
@@ -6347,6 +6351,50 @@ static int endpoint_allocate_eids(struct peer *peer)
63476351
return 0;
63486352
}
63496353

6354+
#if HAVE_LIBCAP
6355+
6356+
static int drop_bind_cap(void)
6357+
{
6358+
cap_flag_t flags[] = { CAP_EFFECTIVE, CAP_INHERITABLE, CAP_PERMITTED };
6359+
cap_value_t bind = CAP_NET_BIND_SERVICE;
6360+
cap_t cap;
6361+
size_t i;
6362+
int rc;
6363+
6364+
cap = cap_get_proc();
6365+
if (!cap)
6366+
return -errno;
6367+
6368+
for (i = 0; i < ARRAY_SIZE(flags); i++) {
6369+
rc = cap_set_flag(cap, flags[i], 1, &bind, CAP_CLEAR);
6370+
if (rc < 0) {
6371+
rc = -errno;
6372+
cap_free(cap);
6373+
return rc;
6374+
}
6375+
}
6376+
6377+
rc = cap_set_proc(cap);
6378+
if (rc < 0) {
6379+
rc = -errno;
6380+
cap_free(cap);
6381+
return rc;
6382+
}
6383+
6384+
rc = cap_free(cap);
6385+
if (rc < 0)
6386+
return -errno;
6387+
6388+
return 0;
6389+
}
6390+
#else
6391+
6392+
static int drop_bind_cap(void)
6393+
{
6394+
return 0;
6395+
}
6396+
#endif
6397+
63506398
int main(int argc, char **argv)
63516399
{
63526400
struct ctx ctxi = { 0 }, *ctx = &ctxi;
@@ -6402,6 +6450,13 @@ int main(int argc, char **argv)
64026450
return 1;
64036451
}
64046452

6453+
rc = drop_bind_cap();
6454+
if (rc < 0) {
6455+
warnx("Error dropping capabilities, returned %s %d",
6456+
strerror(-rc), rc);
6457+
return 1;
6458+
}
6459+
64056460
// All setup must be complete by here, we might immediately
64066461
// get requests from waiting clients.
64076462
rc = request_dbus(ctx);

0 commit comments

Comments
 (0)