Hi,

I gave it another go to further reduce the pledge promises in route(8),
and this is what I could come up with:

Remove the initial pledge and join the 2 switch cases, then apply a
specific pledge depending on the codepath:

flush route, show and monitor use "stdio" if -n is used, otherwise it
uses "stdio rpath dns".

add/change/delete route on the other hand cannot be done with this
condition since nflag is not verified anywhere in the newroute()
function, and it doesn't matter if it's used or not. That being said it
starts with "stdio rpath dns" and after the loop to parse the arguments
and modifiers it can be reduced to "stdio".

While here I also changed 0 to SHUT_RD in order to use the symbolic name
instead of the hardcoded value.

As a side note I inspected route(8)'s source code on FreeBSD and NetBSD
and they also suffer from the same "problem" with nflag when changing
routes, maybe it's still there just for compatibility? As far as I can
remember I never used -n when changing routes because it just works. I
don't think it should be reported to bugs@ though since it's not an
issue per se.

Also adding mikeb@, bennob@ and claudio@ to the conversation as per
theo@'s advise.

Index: route.c
===================================================================
RCS file: /cvs/src/sbin/route/route.c,v
retrieving revision 1.179
diff -u -p -u -r1.179 route.c
--- route.c     25 Oct 2015 09:37:08 -0000      1.179
+++ route.c     19 Nov 2015 14:46:32 -0000
@@ -224,17 +224,6 @@ main(int argc, char **argv)
        case K_FLUSH:
                exit(flushroutes(argc, argv));
                break;
-       }
-               
-       if (nflag) {
-               if (pledge("stdio rpath dns", NULL) == -1)
-                       err(1, "pledge");
-       } else {
-               if (pledge("stdio rpath dns", NULL) == -1)
-                       err(1, "pledge");
-       }
-
-       switch (kw) {
        case K_GET:
                uid = 0;
                /* FALLTHROUGH */
@@ -330,7 +319,7 @@ flushroutes(int argc, char **argv)
        }

        if (nflag) {
-               if (pledge("stdio rpath dns", NULL) == -1)
+               if (pledge("stdio", NULL) == -1)
                        err(1, "pledge");
        } else {
                if (pledge("stdio rpath dns", NULL) == -1)
@@ -445,12 +434,15 @@ newroute(int argc, char **argv)
        int key;
        uint8_t prio = 0;
        struct hostent *hp = NULL;
+       
+       if (pledge("stdio rpath dns", NULL) == -1)
+               err(1, "pledge");

        if (uid)
                errx(1, "must be root to alter routing table");
        cmd = argv[0];
        if (*cmd != 'g')
-               shutdown(s, 0); /* Don't want to read back our messages */
+               shutdown(s, SHUT_RD); /* Don't want to read back our messages */
        while (--argc > 0) {
                if (**(++argv)== '-') {
                        switch (key = keyword(1 + *argv)) {
@@ -630,6 +622,10 @@ newroute(int argc, char **argv)
                                usage(NULL);
                }
        }
+       
+       if (pledge("stdio", NULL) == -1)
+               err(1, "pledge");
+
        if (forcehost)
                ishost = 1;
        if (forcenet)
@@ -1090,8 +1086,13 @@ monitor(int argc, char *argv[])
        char msg[2048];
        time_t now;

-       if (pledge("stdio rpath dns", NULL) == -1)
-               err(1, "pledge");
+       if (nflag) {
+               if (pledge("stdio", NULL) == -1)
+                       err(1, "pledge");
+       } else {
+               if (pledge("stdio rpath dns", NULL) == -1)
+                       err(1, "pledge");
+       }

        verbose = 1;
        if (debugonly) {
Index: show.c
===================================================================
RCS file: /cvs/src/sbin/route/show.c,v
retrieving revision 1.102
diff -u -p -u -r1.102 show.c
--- show.c      23 Oct 2015 15:03:25 -0000      1.102
+++ show.c      19 Nov 2015 14:46:35 -0000
@@ -146,7 +146,7 @@ p_rttables(int af, u_int tableid, int ha
        }

        if (nflag) {
-               if (pledge("stdio rpath dns", NULL) == -1)
+               if (pledge("stdio", NULL) == -1)
                        err(1, "pledge");
        } else {
                if (pledge("stdio rpath dns", NULL) == -1)

Reply via email to