[Toybox] [PATCH] hostname: fix behavior when in jail.

enh enh at google.com
Sat Jan 19 11:41:46 PST 2019


Only -d and -f should cause a DNS lookup. The rest should just act
directly on the result of gethostname(3). Encountered with the AOSP
buildbots' use of nsjail, but tested with both the Debian hostname and
toybox hostname thus:

```
unshare -Uunr sh
hostname android-build
hostname
hostname -s
hostname -d
hostname -f
```

(Not sure how to add that to the tests.)

Also fix a SEGV with -s if the hostname doesn't contain a '.'.

Also switch to the FLAG() macro.

Also add the missing -s to the synopsis.

Bug: http://b/123123255
---
 toys/lsb/hostname.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/toys/lsb/hostname.c b/toys/lsb/hostname.c
index c9fafa54..ac7f9163 100644
--- a/toys/lsb/hostname.c
+++ b/toys/lsb/hostname.c
@@ -10,7 +10,7 @@ config HOSTNAME
   bool "hostname"
   default y
   help
-    usage: hostname [-bsf] [-F FILENAME] [newname]
+    usage: hostname [-bdsf] [-F FILENAME] [newname]

     Get/set the current hostname.

@@ -36,13 +36,13 @@ void hostname_main(void)
   if (TT.F && (hostname = xreadfile(TT.F, 0, 0))) {
     if (!*chomp(hostname)) {
       if (CFG_TOYBOX_FREE) free(hostname);
-      if (!(toys.optflags&FLAG_b)) error_exit("empty '%s'", TT.F);
+      if (!FLAG(b)) error_exit("empty '%s'", TT.F);
       hostname = 0;
     }
   }

   // Implement -b.
-  if (!hostname && (toys.optflags&FLAG_b))
+  if (!hostname && FLAG(b))
     if (gethostname(toybuf, sizeof(toybuf)-1) || !*toybuf)
       hostname = "localhost";

@@ -55,9 +55,12 @@ void hostname_main(void)

   // Get the hostname.
   if (gethostname(toybuf, sizeof(toybuf)-1)) perror_exit("gethostname");
-  if (!(h = gethostbyname(toybuf))) perror_exit("gethostbyname");
-  snprintf(toybuf, sizeof(toybuf), "%s", h->h_name);
+  // We only do the DNS lookup for -d and -f.
+  if (FLAG(d) || FLAG(f)) {
+    if (!(h = gethostbyname(toybuf))) perror_exit("gethostbyname");
+    snprintf(toybuf, sizeof(toybuf), "%s", h->h_name);
+  }
   dot = strchr(toybuf, '.');
-  if (toys.optflags&FLAG_s) *dot = '\0';
-  xputs(toys.optflags&FLAG_d ? dot+1 : toybuf);
+  if (FLAG(s) && dot) *dot = '\0';
+  xputs(FLAG(d) ? dot+1 : toybuf);
 }
-- 
2.20.1.321.g9e740568ce-goog
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-hostname-fix-behavior-when-in-jail.patch
Type: text/x-patch
Size: 2401 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20190119/5598965f/attachment-0002.bin>


More information about the Toybox mailing list