On 2020-07-09, Theo de Raadt wrote:
> Hang on.  Do people ever want to force time calls, outside of ktrace?
> I doubt it.  Should ktrace maybe have a flag, similar to -B with LD_BIND_NOW,
> which sets the new environment variable?  Maybe -T?  The problem smells very
> similar to the root cause for LD_BIND_NOW setting..

So taking into account most feedback, another round.

env variable is now _LIBC_NOUSERTC. It's not libc's concern that ktrace may
want this, so I didn't go with that. The feature in question is usertc,
so that seems the most direct name.

Call getenv before issetugid.

Added a -T option to ktrace for transparency. I got ambitious here and made
it take suboptions, anticipating that other transparency modifications may
be desired. Fold in -B perhaps? I think typing -Tt is not so burdensome,
and saves us some alphabet down the line.

As a bonus, fix ktrace usage. -B only works with second synopsis, with command.


Index: lib/libc/dlfcn/init.c
===================================================================
RCS file: /home/cvs/src/lib/libc/dlfcn/init.c,v
retrieving revision 1.8
diff -u -p -r1.8 init.c
--- lib/libc/dlfcn/init.c       6 Jul 2020 13:33:05 -0000       1.8
+++ lib/libc/dlfcn/init.c       10 Jul 2020 02:12:41 -0000
@@ -114,6 +114,8 @@ _libc_preinit(int argc, char **argv, cha
                                    _timekeep->tk_version != TK_VERSION)
                                        _timekeep = NULL;
                        }
+                       if (getenv("_LIBC_NOUSERTC") && issetugid() == 0)
+                               _timekeep = NULL;
                        break;
                }
        }
Index: usr.bin/ktrace/extern.h
===================================================================
RCS file: /home/cvs/src/usr.bin/ktrace/extern.h,v
retrieving revision 1.4
diff -u -p -r1.4 extern.h
--- usr.bin/ktrace/extern.h     26 Apr 2018 18:30:36 -0000      1.4
+++ usr.bin/ktrace/extern.h     10 Jul 2020 02:21:37 -0000
@@ -26,3 +26,4 @@
  */
 
 int getpoints(const char *, int);
+int gettrans(const char *, int);
Index: usr.bin/ktrace/ktrace.1
===================================================================
RCS file: /home/cvs/src/usr.bin/ktrace/ktrace.1,v
retrieving revision 1.30
diff -u -p -r1.30 ktrace.1
--- usr.bin/ktrace/ktrace.1     15 May 2019 15:36:59 -0000      1.30
+++ usr.bin/ktrace/ktrace.1     10 Jul 2020 02:24:04 -0000
@@ -37,14 +37,15 @@
 .Nd enable kernel process tracing
 .Sh SYNOPSIS
 .Nm ktrace
-.Op Fl aBCcdi
+.Op Fl aCcdi
 .Op Fl f Ar trfile
 .Op Fl g Ar pgid
 .Op Fl p Ar pid
 .Op Fl t Ar trstr
 .Nm ktrace
-.Op Fl adi
+.Op Fl aBdi
 .Op Fl f Ar trfile
+.Op Fl T Ar transopts
 .Op Fl t Ar trstr
 .Ar command
 .Sh DESCRIPTION
@@ -109,6 +110,15 @@ processes.
 Enable (disable) tracing on the indicated process ID (only one
 .Fl p
 flag is permitted).
+.It Fl T Ar transopts
+Request additional transparency from libc.
+By default, no options are enabled.
+The argument can contain one or more of the following letters.
+.Pp
+.Bl -tag -width flag -offset indent -compact
+.It Cm t
+Disable userland timecounters so that time related system calls are visible.
+.El
 .It Fl t Ar trstr
 Select which information to put into the dump file.
 The argument can contain one or more of the following letters.
Index: usr.bin/ktrace/ktrace.c
===================================================================
RCS file: /home/cvs/src/usr.bin/ktrace/ktrace.c,v
retrieving revision 1.36
diff -u -p -r1.36 ktrace.c
--- usr.bin/ktrace/ktrace.c     28 Jun 2019 13:35:01 -0000      1.36
+++ usr.bin/ktrace/ktrace.c     10 Jul 2020 02:23:33 -0000
@@ -60,7 +60,7 @@ int
 main(int argc, char *argv[])
 {
        enum { NOTSET, CLEAR, CLEARALL } clear;
-       int append, ch, fd, inherit, ops, pidset, trpoints;
+       int append, ch, fd, inherit, ops, pidset, trpoints, transparency;
        pid_t pid;
        char *tracefile, *tracespec;
        mode_t omask;
@@ -71,6 +71,7 @@ main(int argc, char *argv[])
        clear = NOTSET;
        append = ops = pidset = inherit = pid = 0;
        trpoints = is_ltrace ? KTRFAC_USER : DEF_POINTS;
+       transparency = 0;
        tracefile = DEF_TRACEFILE;
        tracespec = NULL;
 
@@ -100,7 +101,7 @@ main(int argc, char *argv[])
                                usage();
                        }
        } else {
-               while ((ch = getopt(argc, argv, "aBCcdf:g:ip:t:")) != -1)
+               while ((ch = getopt(argc, argv, "aBCcdf:g:ip:t:T:")) != -1)
                        switch ((char)ch) {
                        case 'a':
                                append = 1;
@@ -140,6 +141,13 @@ main(int argc, char *argv[])
                                        usage();
                                }
                                break;
+                       case 'T':
+                               transparency = gettrans(optarg, 0);
+                               if (transparency < 0) {
+                                       warnx("unknown facility in %s", optarg);
+                                       usage();
+                               }
+                               break;
                        default:
                                usage();
                        }
@@ -154,6 +162,9 @@ main(int argc, char *argv[])
        if (inherit)
                trpoints |= KTRFAC_INHERIT;
 
+       if (transparency && TRANS_TIME)
+               putenv("_LIBC_NOUSERTC=");
+
        (void)signal(SIGSYS, no_ktrace);
        if (clear != NOTSET) {
                if (clear == CLEARALL) {
@@ -240,9 +251,9 @@ usage(void)
                    " [-u trspec] command\n",
                    __progname);
        else
-               fprintf(stderr, "usage: %s [-aBCcdi] [-f trfile] [-g pgid]"
+               fprintf(stderr, "usage: %s [-aCcdi] [-f trfile] [-g pgid]"
                    " [-p pid] [-t trstr]\n"
-                   "       %s [-adi] [-f trfile] [-t trstr] command\n",
+                   "       %s [-aBdi] [-f trfile] [-T transopts] [-t trstr] 
command\n",
                    __progname, __progname);
        exit(1);
 }
Index: usr.bin/ktrace/ktrace.h
===================================================================
RCS file: /home/cvs/src/usr.bin/ktrace/ktrace.h,v
retrieving revision 1.10
diff -u -p -r1.10 ktrace.h
--- usr.bin/ktrace/ktrace.h     6 Mar 2016 20:25:27 -0000       1.10
+++ usr.bin/ktrace/ktrace.h     10 Jul 2020 02:18:01 -0000
@@ -38,3 +38,5 @@
 #define ALL_POINTS (DEF_POINTS | KTRFAC_EXECENV)
 
 #define DEF_TRACEFILE  "ktrace.out"
+
+#define        TRANS_TIME      1
Index: usr.bin/ktrace/subr.c
===================================================================
RCS file: /home/cvs/src/usr.bin/ktrace/subr.c,v
retrieving revision 1.14
diff -u -p -r1.14 subr.c
--- usr.bin/ktrace/subr.c       26 Apr 2018 12:42:51 -0000      1.14
+++ usr.bin/ktrace/subr.c       10 Jul 2020 02:15:44 -0000
@@ -86,3 +86,24 @@ getpoints(const char *s, int defpoints)
        }
        return (facs);
 }
+
+int
+gettrans(const char *s, int deftrans)
+{
+       int trans = 0;
+
+       while (*s) {
+               switch(*s) {
+               case 't':
+                       trans |= TRANS_TIME;
+                       break;
+               case '+':
+                       trans |= deftrans;
+                       break;
+               default:
+                       return (-1);
+               }
+               s++;
+       }
+       return (trans);
+}

Reply via email to