1 From 7ea2433d716172f160c9380ed0bb852fafe845a2 Mon Sep 17 00:00:00 2001
2 From: Pavel Zhukov <pzhukov@redhat.com>
3 Date: Thu, 21 Feb 2019 10:30:28 +0100
4 Subject: [PATCH 11/28] Drop unnecessary capabilities
6 dhclient (#517649, #546765), dhcpd/dhcrelay (#699713)
8 client/Makefile.am | 3 ++-
9 client/dhclient-script.8 | 10 ++++++++++
10 client/dhclient.8 | 29 +++++++++++++++++++++++++++++
11 client/dhclient.c | 24 ++++++++++++++++++++++++
12 configure.ac | 35 +++++++++++++++++++++++++++++++++++
13 relay/Makefile.am | 3 ++-
14 relay/dhcrelay.c | 29 +++++++++++++++++++++++++++++
15 7 files changed, 131 insertions(+), 2 deletions(-)
17 diff -urNp -x '*.orig' dhcp-4.4.3.org/client/Makefile.am dhcp-4.4.3/client/Makefile.am
18 --- dhcp-4.4.3.org/client/Makefile.am 2022-03-08 09:26:03.000000000 +0000
19 +++ dhcp-4.4.3/client/Makefile.am 2022-04-03 17:02:38.643517435 +0000
20 @@ -17,6 +17,7 @@ dhclient_LDADD = ../common/libdhcp.@A@ .
21 @BINDLIBIRSDIR@/libirs.@A@ \
22 @BINDLIBDNSDIR@/libdns.@A@ \
23 @BINDLIBISCCFGDIR@/libisccfg.@A@ \
24 - @BINDLIBISCDIR@/libisc.@A@
25 + @BINDLIBISCDIR@/libisc.@A@ \
27 man_MANS = dhclient.8 dhclient-script.8 dhclient.conf.5 dhclient.leases.5
28 EXTRA_DIST = $(man_MANS)
29 diff -urNp -x '*.orig' dhcp-4.4.3.org/client/dhclient-script.8 dhcp-4.4.3/client/dhclient-script.8
30 --- dhcp-4.4.3.org/client/dhclient-script.8 2022-04-03 17:02:38.454517007 +0000
31 +++ dhcp-4.4.3/client/dhclient-script.8 2022-04-03 17:02:38.643517435 +0000
32 @@ -249,6 +249,16 @@ repeatedly initialized to the values pro
33 the other. Assuming the information provided by both servers is
34 valid, this shouldn't cause any real problems, but it could be
37 +Normally, if dhclient was compiled with libcap-ng support,
38 +dhclient drops most capabilities immediately upon startup.
39 +While more secure, this greatly restricts the additional actions that
40 +hooks in dhclient-script can take. For example, any daemons that
41 +dhclient-script starts or restarts will inherit the restricted
42 +capabilities as well, which may interfere with their correct operation.
45 +option can be used to prevent dhclient from dropping capabilities.
47 dhclient(8), dhcpd(8), dhcrelay(8), dhclient.conf(5) and
49 diff -urNp -x '*.orig' dhcp-4.4.3.org/client/dhclient.8 dhcp-4.4.3/client/dhclient.8
50 --- dhcp-4.4.3.org/client/dhclient.8 2022-04-03 17:02:38.455517009 +0000
51 +++ dhcp-4.4.3/client/dhclient.8 2022-04-03 17:02:38.643517435 +0000
52 @@ -135,6 +135,9 @@ dhclient - Dynamic Host Configuration Pr
62 @@ -330,6 +333,32 @@ program can then be used to notify the c
63 has been added or removed, so that the client can attempt to configure an IP
64 address on that interface.
67 +Do not drop capabilities.
71 +was compiled with libcap-ng support,
73 +drops most capabilities immediately upon startup. While more secure,
74 +this greatly restricts the additional actions that hooks in
75 +.B dhclient-script (8)
76 +can take. (For example, any daemons that
77 +.B dhclient-script (8)
78 +starts or restarts will inherit the restricted capabilities as well,
79 +which may interfere with their correct operation.) Thus, the
81 +option can be used to prevent
83 +from dropping capabilities.
89 +was not compiled with libcap-ng support.
93 Do not configure any interfaces. This is most likely to be useful in
95 diff -urNp -x '*.orig' dhcp-4.4.3.org/client/dhclient.c dhcp-4.4.3/client/dhclient.c
96 --- dhcp-4.4.3.org/client/dhclient.c 2022-04-03 17:02:38.461517023 +0000
97 +++ dhcp-4.4.3/client/dhclient.c 2022-04-03 17:02:38.644517437 +0000
102 +#ifdef HAVE_LIBCAP_NG
107 * Defined in stdio.h when _GNU_SOURCE is set, but we don't want to define
108 * that when building ISC code.
109 @@ -269,6 +273,9 @@ main(int argc, char **argv) {
111 char *arg_conf = NULL;
112 int arg_conf_len = 0;
113 +#ifdef HAVE_LIBCAP_NG
114 + int keep_capabilities = 0;
117 /* Initialize client globals. */
118 memset(&default_duid, 0, sizeof(default_duid));
119 @@ -668,6 +675,10 @@ main(int argc, char **argv) {
121 dhclient_request_options = argv[i];
123 + } else if (!strcmp(argv[i], "-nc")) {
124 +#ifdef HAVE_LIBCAP_NG
125 + keep_capabilities = 1;
127 } else if (argv[i][0] == '-') {
128 usage("Unknown command: %s", argv[i]);
129 } else if (interfaces_requested < 0) {
130 @@ -728,6 +739,19 @@ main(int argc, char **argv) {
131 path_dhclient_script = s;
134 +#ifdef HAVE_LIBCAP_NG
135 + /* Drop capabilities */
136 + if (!keep_capabilities) {
137 + capng_clear(CAPNG_SELECT_CAPS);
138 + capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
139 + CAP_DAC_OVERRIDE); // Drop this someday
140 + capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
141 + CAP_NET_ADMIN, CAP_NET_RAW,
142 + CAP_NET_BIND_SERVICE, CAP_SYS_ADMIN, -1);
143 + capng_apply(CAPNG_SELECT_CAPS);
147 /* Set up the initial dhcp option universe. */
148 initialize_common_option_spaces();
150 diff -urNp -x '*.orig' dhcp-4.4.3.org/configure.ac dhcp-4.4.3/configure.ac
151 --- dhcp-4.4.3.org/configure.ac 2022-04-03 17:02:38.459517018 +0000
152 +++ dhcp-4.4.3/configure.ac 2022-04-03 17:02:38.644517437 +0000
153 @@ -603,6 +603,41 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]],
154 # Look for optional headers.
155 AC_CHECK_HEADERS(sys/socket.h net/if_dl.h net/if6.h regex.h)
157 +# look for capabilities library
158 +AC_ARG_WITH(libcap-ng,
159 + [ --with-libcap-ng=[auto/yes/no] Add Libcap-ng support [default=auto]],,
160 + with_libcap_ng=auto)
162 +# Check for Libcap-ng API
164 +# libcap-ng detection
165 +if test x$with_libcap_ng = xno ; then
168 + # Start by checking for header file
169 + AC_CHECK_HEADER(cap-ng.h, capng_headers=yes, capng_headers=no)
171 + # See if we have libcap-ng library
172 + AC_CHECK_LIB(cap-ng, capng_clear,
173 + CAPNG_LDADD=-lcap-ng,)
175 + # Check results are usable
176 + if test x$with_libcap_ng = xyes -a x$CAPNG_LDADD = x ; then
177 + AC_MSG_ERROR(libcap-ng support was requested and the library was not found)
179 + if test x$CAPNG_LDADD != x -a $capng_headers = no ; then
180 + AC_MSG_ERROR(libcap-ng libraries found but headers are missing)
183 +AC_SUBST(CAPNG_LDADD)
184 +AC_MSG_CHECKING(whether to use libcap-ng)
185 +if test x$CAPNG_LDADD != x ; then
186 + AC_DEFINE(HAVE_LIBCAP_NG,1,[libcap-ng support])
192 # Solaris needs some libraries for functions
193 AC_SEARCH_LIBS(socket, [socket])
194 AC_SEARCH_LIBS(inet_ntoa, [nsl])
195 diff -urNp -x '*.orig' dhcp-4.4.3.org/relay/Makefile.am dhcp-4.4.3/relay/Makefile.am
196 --- dhcp-4.4.3.org/relay/Makefile.am 2022-03-08 09:26:03.000000000 +0000
197 +++ dhcp-4.4.3/relay/Makefile.am 2022-04-03 17:02:38.644517437 +0000
198 @@ -8,6 +8,7 @@ dhcrelay_LDADD = ../common/libdhcp.@A@ .
199 @BINDLIBIRSDIR@/libirs.@A@ \
200 @BINDLIBDNSDIR@/libdns.@A@ \
201 @BINDLIBISCCFGDIR@/libisccfg.@A@ \
202 - @BINDLIBISCDIR@/libisc.@A@
203 + @BINDLIBISCDIR@/libisc.@A@ \
205 man_MANS = dhcrelay.8
206 EXTRA_DIST = $(man_MANS)
207 diff -urNp -x '*.orig' dhcp-4.4.3.org/relay/dhcrelay.c dhcp-4.4.3/relay/dhcrelay.c
208 --- dhcp-4.4.3.org/relay/dhcrelay.c 2022-04-03 17:02:38.462517025 +0000
209 +++ dhcp-4.4.3/relay/dhcrelay.c 2022-04-03 17:02:38.645517439 +0000
211 #include <systemd/sd-daemon.h>
214 +#ifdef HAVE_LIBCAP_NG
215 +# include <cap-ng.h>
216 + int keep_capabilities = 0;
219 TIME default_lease_time = 43200; /* 12 hours... */
220 TIME max_lease_time = 86400; /* 24 hours... */
221 struct tree_cache *global_options[256];
222 @@ -616,6 +621,10 @@ main(int argc, char **argv) {
223 usage(use_noarg, argv[i-1]);
224 dhcrelay_sub_id = argv[i];
226 + } else if (!strcmp(argv[i], "-nc")) {
227 +#ifdef HAVE_LIBCAP_NG
228 + keep_capabilities = 1;
230 } else if (!strcmp(argv[i], "-pf")) {
232 usage(use_noarg, argv[i-1]);
233 @@ -685,6 +694,17 @@ main(int argc, char **argv) {
237 +#ifdef HAVE_LIBCAP_NG
238 + /* Drop capabilities */
239 + if (!keep_capabilities) {
240 + capng_clear(CAPNG_SELECT_BOTH);
241 + capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
242 + CAP_NET_RAW, CAP_NET_BIND_SERVICE, -1);
243 + capng_apply(CAPNG_SELECT_BOTH);
244 + log_info ("Dropped all unnecessary capabilities.");
249 log_info("%s %s", message, PACKAGE_VERSION);
251 @@ -849,6 +869,15 @@ main(int argc, char **argv) {
252 (unsigned long) getpid());
255 +#ifdef HAVE_LIBCAP_NG
256 + /* Drop all capabilities */
257 + if (!keep_capabilities) {
258 + capng_clear(CAPNG_SELECT_BOTH);
259 + capng_apply(CAPNG_SELECT_BOTH);
260 + log_info ("Dropped all capabilities.");
264 /* Start dispatching packets and timeouts... */