Skip to content

Commit

Permalink
Use systemd's sd_notify with option up_sdnotify
Browse files Browse the repository at this point in the history
This adds an up_sdnotify option so that systemd services of
Type=notify can have pppd send the READY=1 signal to systemd
once a network protocol (typically IP) is up.

To use up_sdnotify, pppd must be compiled with SYSTEMD=y.

up_sdnotify is safe as a non-priveleged option because systemd will
ignore any notifications that it is not expecting. If systemd starts
pppd in a unit-file that is Type=notify, then (and only then) will it
handle the READY=1 signal. If systemd didn't start the process, it
ignroes any notifications unless the signaling process was started by a
service that systemd is monitoring (directly or indirectly, such as a
grandchild process in the same cgroup as a process that systemd started)
AND that service is Type=notify, AND that service is explicitly
configured to allow other processes to send a notification on behalf of
that service by setting NotifyAccess=all.

Also, the socket used is defined in an environment variable provided and
deleted by systemd, allowing system and user services to use a different
socket. I really don't think there's any way to use that socket (even via
the sd_notify api of their library) to gain elevated privileges.

Another reason that up_sdnotify is a non-priveleged option is for cases
where ppp should be started as a system service under a non-priveleged
account. There may be other issues with running ppp under other
accounts, but systemd does not require root--or other privileged--access
in order to use the notification feature. Instead the security for this
feature is provided at the process level in that systemd knows which
processes it did and did not start, and which processes those processes
started (ie other processes in the systemd unit's cgroup), as explained
above.

Signed-off-by: Jacob Floyd <[email protected]>
  • Loading branch information
cognifloyd committed Feb 27, 2018
1 parent a3f379b commit d34159f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pppd/Makefile.linux
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ HAVE_MULTILINK=y
# Linux distributions: Please leave TDB ENABLED in your builds.
USE_TDB=y

# Uncomment the next line to enable Type=notify services in systemd
# If enabled, and the user sets the up_sdnotify option, then
# pppd will not detach and will notify systemd when up.
#SYSTEMD=y

HAS_SHADOW=y
#USE_PAM=y
HAVE_INET6=y
Expand Down Expand Up @@ -170,6 +175,11 @@ LIBS += -llock
CFLAGS += -DLOCKLIB=1
endif

ifdef SYSTEMD
LIBS += -lsystemd
CFLAGS += -DSYSTEMD=1
endif

ifdef PLUGIN
CFLAGS += -DPLUGIN
LDFLAGS += -Wl,-E
Expand Down
13 changes: 12 additions & 1 deletion pppd/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
#endif
#include <time.h>

#ifdef SYSTEMD
#include <systemd/sd-daemon.h>
#endif

#include "pppd.h"
#include "fsm.h"
#include "lcp.h"
Expand Down Expand Up @@ -1099,8 +1103,15 @@ np_up(unit, proto)
/*
* Detach now, if the updetach option was given.
*/
if (updetach && !nodetach)
if (updetach && !nodetach) {
dbglog("updetach is set. Now detaching.");
detach();
#ifdef SYSTEMD
} else if (nodetach && up_sdnotify) {
dbglog("up_sdnotify is set. Now notifying systemd: READY=1");
sd_notify(0, "READY=1");
#endif
}
}
++num_np_up;
}
Expand Down
8 changes: 8 additions & 0 deletions pppd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ char devnam[MAXPATHLEN]; /* Device name */
bool nodetach = 0; /* Don't detach from controlling tty */
bool updetach = 0; /* Detach once link is up */
bool master_detach; /* Detach when we're (only) multilink master */
#ifdef SYSTEMD
bool up_sdnotify = 0; /* Notify systemd once link is up */
#endif
int maxconnect = 0; /* Maximum connect time */
char user[MAXNAMELEN]; /* Username for PAP */
char passwd[MAXSECRETLEN]; /* Password for PAP */
Expand Down Expand Up @@ -209,6 +212,11 @@ option_t general_options[] = {
"Don't detach from controlling tty", OPT_PRIO | 1 },
{ "-detach", o_bool, &nodetach,
"Don't detach from controlling tty", OPT_ALIAS | OPT_PRIOSUB | 1 },
#ifdef SYSTEMD
{ "up_sdnotify", o_bool, &up_sdnotify,
"Notify systemd once link is up (implies nodetach)",
OPT_PRIOSUB | OPT_A2COPY | 1, &nodetach },
#endif
{ "updetach", o_bool, &updetach,
"Detach from controlling tty once link is up",
OPT_PRIOSUB | OPT_A2CLR | 1, &nodetach },
Expand Down
9 changes: 9 additions & 0 deletions pppd/pppd.8
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,15 @@ it has successfully established the ppp connection (to the point where
the first network control protocol, usually the IP control protocol,
has come up).
.TP
.B up_sdnotify
Use this option to run pppd in systemd service units of Type=notify
(\fBup_sdnotify\fR implies \fBnodetach\fR).
When \fBup_sdnotify\fR is enabled, pppd will notify systemd once
it has successfully established the ppp connection (to the point where
the first network control protocl, usually the IP control protocol,
has come up). This option is only availble when pppd is compiled with
systemd support.
.TP
.B usehostname
Enforce the use of the hostname (with domain name appended, if given)
as the name of the local system for authentication purposes (overrides
Expand Down
3 changes: 3 additions & 0 deletions pppd/pppd.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ extern int inspeed; /* Input/Output speed requested */
extern u_int32_t netmask; /* IP netmask to set on interface */
extern bool lockflag; /* Create lock file to lock the serial dev */
extern bool nodetach; /* Don't detach from controlling tty */
#ifdef SYSTEMD
extern bool up_sdnotify; /* Notify systemd once link is up (implies nodetach) */
#endif
extern bool updetach; /* Detach from controlling tty when link up */
extern bool master_detach; /* Detach when multilink master without link */
extern char *initializer; /* Script to initialize physical link */
Expand Down

0 comments on commit d34159f

Please sign in to comment.