Don't allow rm -rf /

2015-08-23 Thread Theo Buehler
In episode 103 of BSD Now, Bryan Cantrill talks about the fact that
on illumos rm -rf / is an error.  It turns out that this behavior
is mandated by POSIX 1003.1-2013:

If either of the files dot or dot-dot are specified as the basename
portion of an operand (that is, the final pathname component) or if an
operand resolves to the root directory, rm shall write a diagnostic
message to standard error and do nothing more with such operands.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/rm.html

On FreeBSD, a check for / only happens if POSIXLY_CORRECT is set,
while on NetBSD, there currently is no such check at all.

Index: bin/rm/rm.1
===
RCS file: /var/cvs/src/bin/rm/rm.1,v
retrieving revision 1.37
diff -u -p -r1.37 rm.1
--- bin/rm/rm.1 25 May 2014 19:07:36 -  1.37
+++ bin/rm/rm.1 22 Aug 2015 21:49:02 -
@@ -102,6 +102,7 @@ The
 utility removes symbolic links, not the files referenced by the links.
 .Pp
 It is an error to attempt to remove the files
+.Dq / ,
 .Dq \.
 or
 .Dq .. .
Index: bin/rm/rm.c
===
RCS file: /var/cvs/src/bin/rm/rm.c,v
retrieving revision 1.30
diff -u -p -r1.30 rm.c
--- bin/rm/rm.c 16 Jan 2015 06:39:32 -  1.30
+++ bin/rm/rm.c 22 Aug 2015 21:58:28 -
@@ -54,7 +54,7 @@ extern char *__progname;
 int dflag, eval, fflag, iflag, Pflag, stdin_ok;
 
 intcheck(char *, char *, struct stat *);
-void   checkdot(char **);
+void   checkdotorslash(char **);
 void   rm_file(char **);
 intrm_overwrite(char *, struct stat *);
 intpass(int, off_t, char *, size_t);
@@ -105,7 +105,7 @@ main(int argc, char *argv[])
if (argc  1  fflag == 0)
usage();
 
-   checkdot(argv);
+   checkdotorslash(argv);
 
if (*argv) {
stdin_ok = isatty(STDIN_FILENO);
@@ -383,7 +383,7 @@ check(char *path, char *name, struct sta
  */
 #define ISDOT(a)   ((a)[0] == '.'  (!(a)[1] || ((a)[1] == '.'  
!(a)[2])))
 void
-checkdot(char **argv)
+checkdotorslash(char **argv)
 {
char *p, **save, **t;
int complained;
@@ -401,9 +401,10 @@ checkdot(char **argv)
else
p = *t;
 
-   if (ISDOT(p)) {
+   if (ISDOT(p) || *p == '\0') {
if (!complained++)
-   warnx(\.\ and \..\ may not be removed);
+   warnx(\/\, \.\ and \..\ may not be 
+   removed);
eval = 1;
for (save = t; (t[0] = t[1]) != NULL; ++t)
continue;



document clear(1) a bit more prominently

2015-08-23 Thread Theo Buehler
The clear(1) manpage is linked to the tput(1) manpage, but the clear
utility is only mentioned in the history section.  I suggest to make its
appearance in the manual page a little more prominent.

Index: tput.1
===
RCS file: /var/cvs/src/usr.bin/tput/tput.1,v
retrieving revision 1.21
diff -u -p -r1.21 tput.1
--- tput.1  3 Jun 2014 20:28:24 -   1.21
+++ tput.1  4 Aug 2015 00:28:24 -
@@ -34,7 +34,8 @@
 .Dt TPUT 1
 .Os
 .Sh NAME
-.Nm tput
+.Nm tput ,
+.Nm clear
 .Nd terminal capability interface
 .Sh SYNOPSIS
 .Nm tput
@@ -45,11 +46,17 @@
 .Nm tput
 .Op Fl T Ar term
 .Fl S
+.Nm clear
+.Op Fl T Ar term
 .Sh DESCRIPTION
 The
 .Nm
 utility makes terminal-dependent information available to users or shell
 applications.
+The
+.Nm clear
+utility provides the same functionality as
+.Nm tput Cm clear .
 .Pp
 The options are as follows:
 .Bl -tag -width Ds
@@ -166,11 +173,9 @@ and the attribute
 .Cm longname
 are extensions to that specification.
 .Sh HISTORY
-A
+The
 .Nm clear
-utility implementing the functionality of
-.Nm tput Cm clear
-first appeared in
+utility first appeared in
 .Bx 2 .
 The
 .Nm



opendev(3): Do not mask errno

2015-08-23 Thread Steven McDonald
Hi,

Below is a diff to avoid masking errno from DIOCMAP in opendev(3). The
intention of the existing code appears to be to try using path as a
device name rather than a DUID in case of failure, but as far as I can
tell, this is only desirable if DIOCMAP returns ENOENT anyway. Other
kinds of error indicate a problem with a valid DUID that the user will
likely want to know about and retry.

For example, consider running tunefs against a filesystem that is
currently mounted. DIOCMAP will return EBUSY, but resetting errno to
ENOENT will produce the confusing error message No such file or
directory.

Index: lib/libutil/opendev.c
===
RCS file: /cvs/src/lib/libutil/opendev.c,v
retrieving revision 1.15
diff -u -p -r1.15 opendev.c
--- lib/libutil/opendev.c   30 Jun 2011 15:04:58 -  1.15
+++ lib/libutil/opendev.c   23 Aug 2015 07:27:40 -
@@ -79,7 +79,6 @@ opendev(const char *path, int oflags, in
if (ioctl(fd, DIOCMAP, dm) == -1) {
close(fd);
fd = -1;
-   errno = ENOENT;
}
}
}



Re: tunefs(8): Fix handling of device names

2015-08-23 Thread Steven McDonald
On Sun, 23 Aug 2015 19:21:42 +1000
Steven McDonald ste...@steven-mcdonald.id.au wrote:

 Also, section 6.9.1 clause 9 of C99[0] says that each parameter has
 automatic storage duration, so I think it's not safe to rely on a
 function parameter still being a valid object outside of openpartition
 (please correct me if I'm wrong on that).

Sorry, I just realised that it's fine to use name because it's only the
value pointed to by name that is being used in my diff.

Index: tunefs.c
===
RCS file: /cvs/src/sbin/tunefs/tunefs.c,v
retrieving revision 1.37
diff -u -p -r1.37 tunefs.c
--- tunefs.c7 Feb 2015 02:09:14 -   1.37
+++ tunefs.c23 Aug 2015 09:43:48 -
@@ -309,21 +309,18 @@ bread(daddr_t blk, char *buffer, int cnt
 static int
 openpartition(char *name, int flags, char **devicep)
 {
-   charrawspec[PATH_MAX], *p;
+   char*p;
struct fstab*fs;
int fd;
 
fs = getfsfile(name);
if (fs) {
-   if ((p = strrchr(fs-fs_spec, '/')) != NULL) {
-   snprintf(rawspec, sizeof(rawspec), %.*s/r%s,
-   (int)(p - fs-fs_spec), fs-fs_spec, p + 1);
-   name = rawspec;
-   } else
-   name = fs-fs_spec;
+   name = fs-fs_spec;
+   if ((p = strrchr(name, '/')) != NULL)
+   name = p + 1;
}
fd = opendev(name, flags, 0, devicep);
if (fd == -1  errno == ENOENT)
-   devicep = name;
+   *devicep = name;
return (fd);
 }



Re: Don't allow rm -rf /

2015-08-23 Thread Theo Buehler
On Sun, Aug 23, 2015 at 07:38:15AM -0400, Ted Unangst wrote:
 Theo Buehler wrote:
  @@ -401,9 +401,10 @@ checkdot(char **argv)
  else
  p = *t;
   
  -   if (ISDOT(p)) {
  +   if (ISDOT(p) || *p == '\0') {
  if (!complained++)
  -   warnx(\.\ and \..\ may not be removed);
  +   warnx(\/\, \.\ and \..\ may not be 
  +   removed);
  eval = 1;
  for (save = t; (t[0] = t[1]) != NULL; ++t)
  continue;
  
 
 I think separate error messages are appriate. The one about dots should not be
 cluttered with this.

Do I miss something simpler?

Index: bin/rm/rm.1
===
RCS file: /var/cvs/src/bin/rm/rm.1,v
retrieving revision 1.37
diff -u -p -r1.37 rm.1
--- bin/rm/rm.1 25 May 2014 19:07:36 -  1.37
+++ bin/rm/rm.1 22 Aug 2015 21:49:02 -
@@ -102,6 +102,7 @@ The
 utility removes symbolic links, not the files referenced by the links.
 .Pp
 It is an error to attempt to remove the files
+.Dq / ,
 .Dq \.
 or
 .Dq .. .
Index: bin/rm/rm.c
===
RCS file: /var/cvs/src/bin/rm/rm.c,v
retrieving revision 1.30
diff -u -p -r1.30 rm.c
--- bin/rm/rm.c 16 Jan 2015 06:39:32 -  1.30
+++ bin/rm/rm.c 23 Aug 2015 11:52:28 -
@@ -54,7 +54,7 @@ extern char *__progname;
 int dflag, eval, fflag, iflag, Pflag, stdin_ok;
 
 intcheck(char *, char *, struct stat *);
-void   checkdot(char **);
+void   checkdotorslash(char **);
 void   rm_file(char **);
 intrm_overwrite(char *, struct stat *);
 intpass(int, off_t, char *, size_t);
@@ -105,7 +105,7 @@ main(int argc, char *argv[])
if (argc  1  fflag == 0)
usage();
 
-   checkdot(argv);
+   checkdotorslash(argv);
 
if (*argv) {
stdin_ok = isatty(STDIN_FILENO);
@@ -383,12 +383,12 @@ check(char *path, char *name, struct sta
  */
 #define ISDOT(a)   ((a)[0] == '.'  (!(a)[1] || ((a)[1] == '.'  
!(a)[2])))
 void
-checkdot(char **argv)
+checkdotorslash(char **argv)
 {
char *p, **save, **t;
-   int complained;
+   int dotcomplained, slashcomplained;
 
-   complained = 0;
+   dotcomplained = slashcomplained = 0;
for (t = argv; *t;) {
/* strip trailing slashes */
p = strrchr (*t, '\0');
@@ -402,14 +402,20 @@ checkdot(char **argv)
p = *t;
 
if (ISDOT(p)) {
-   if (!complained++)
+   if (!dotcomplained++)
warnx(\.\ and \..\ may not be removed);
-   eval = 1;
-   for (save = t; (t[0] = t[1]) != NULL; ++t)
-   continue;
-   t = save;
-   } else
+   } else if (*p == '\0') {
+   if (!slashcomplained++)
+   warnx(\/\ may not be removed);
+   } else {
++t;
+   continue;
+   }
+
+   eval = 1;
+   for (save = t; (t[0] = t[1]) != NULL; ++t)
+   continue;
+   t = save;
}
 }
 



tunefs(8): Fix handling of device names

2015-08-23 Thread Steven McDonald
The openpartition function in tunefs(8) will currently set devicep, a
char** passed in from main, to the path it attempted to opendev(3).
This doesn't actually work as intended; it should be setting the value
devicep points to instead of devicep itself.

Also, section 6.9.1 clause 9 of C99[0] says that each parameter has
automatic storage duration, so I think it's not safe to rely on a
function parameter still being a valid object outside of openpartition
(please correct me if I'm wrong on that).

The diff below addresses both of these issues, and while here, also
cleans up some path-wrangling baggage dating from prior to opendev(3).

[0] http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

Index: tunefs.c
===
RCS file: /cvs/src/sbin/tunefs/tunefs.c,v
retrieving revision 1.37
diff -u -p -r1.37 tunefs.c
--- tunefs.c7 Feb 2015 02:09:14 -   1.37
+++ tunefs.c23 Aug 2015 09:11:58 -
@@ -309,21 +309,22 @@ bread(daddr_t blk, char *buffer, int cnt
 static int
 openpartition(char *name, int flags, char **devicep)
 {
-   charrawspec[PATH_MAX], *p;
+   static char realname[PATH_MAX];
+   char*p;
struct fstab*fs;
int fd;
 
fs = getfsfile(name);
if (fs) {
-   if ((p = strrchr(fs-fs_spec, '/')) != NULL) {
-   snprintf(rawspec, sizeof(rawspec), %.*s/r%s,
-   (int)(p - fs-fs_spec), fs-fs_spec, p + 1);
-   name = rawspec;
-   } else
-   name = fs-fs_spec;
+   name = fs-fs_spec;
+   if ((p = strrchr(name, '/')) != NULL)
+   name = p + 1;
}
fd = opendev(name, flags, 0, devicep);
-   if (fd == -1  errno == ENOENT)
-   devicep = name;
+   if (fd == -1  errno == ENOENT) {
+   /* Only used in err(3), truncation is ok. */
+   (void)strlcpy(realname, name, sizeof(realname));
+   *devicep = realname;
+   }
return (fd);
 }



worm(6) remove cheating bug

2015-08-23 Thread Rafael Zalamena
I just fixed a bug which allowed people to cheat in worm(6). This bug was
found out by deraadt@ when peer reviewing the mail thread in tech@
'Fwd: worm.c removing unused variables'.

To reproduce the bug simply hold spacebar and your worm won't move.

Highlights:
 * Use the unused time variables to record how long has been since the
   last valid key press;
 * Changed the process() function to return whether the key pressed was
   valid or not;
 * Use clock_gettime(CLOCK_UPTIME) instead of gettimeofday() to provide
   reliable clock source even on suspend/wake up;

ok?

Index: worm.c
===
RCS file: /cvs/src/games/worm/worm.c,v
retrieving revision 1.30
diff -u -p -r1.30 worm.c
--- worm.c  22 Aug 2015 14:47:41 -  1.30
+++ worm.c  23 Aug 2015 20:42:42 -
@@ -78,7 +78,7 @@ void  leave(int);
 void   life(void);
 void   newpos(struct body *);
 struct body*newlink(void);
-void   process(int);
+intprocess(int);
 void   prize(void);
 intrnd(int);
 void   setup(void);
@@ -88,10 +88,11 @@ int
 main(int argc, char **argv)
 {
int retval;
-   struct timeval t, tod;
-   struct timezone tz;
struct pollfd pfd[1];
const char *errstr;
+   struct timespec t, tn, tdiff;
+
+   memset(t, 0, sizeof(t));
 
setbuf(stdout, outbuf);
signal(SIGINT, leave);
@@ -154,18 +155,34 @@ main(int argc, char **argv)
running--;
process(lastch);
} else {
-   /* fflush(stdout); */
-   /* Delay could be a command line option */
-   t.tv_sec = 1;
-   t.tv_usec = 0;
-   (void)gettimeofday(tod, tz);
+   /* Check for user input timeout. */
+   clock_gettime(CLOCK_UPTIME, tn);
+   if (timespeccmp(t, tn, =)) {
+   t = tn;
+   t.tv_sec += 1;
+
+   process(lastch);
+   continue;
+   }
+
+   /* Prepare next read */
pfd[0].fd = STDIN_FILENO;
pfd[0].events = POLLIN;
-   retval = poll(pfd, 1, t.tv_sec * 1000 + t.tv_usec / 
1000);
-   if (retval  0)
-   process(getch());
-   else
-   process(lastch);
+   timespecsub(t, tn, tdiff);
+   retval = poll(pfd, 1, (tdiff.tv_sec * 1000) +
+   (tdiff.tv_nsec / 100));
+
+   /* Nothing to do if timed out or signal. */
+   if (retval = 0)
+   continue;
+
+   /* Only update timer if valid key was pressed. */
+   if (process(getch()) == 0)
+   continue;
+
+   /* Update using clock_gettime(), tn is too old now. */
+   clock_gettime(CLOCK_UPTIME, t);
+   t.tv_sec += 1;
}
}
 }
@@ -245,7 +262,7 @@ prize(void)
wrefresh(tv);
 }
 
-void
+int
 process(int ch)
 {
int x,y;
@@ -300,21 +317,21 @@ process(int ch)
break;
case '\f':
setup();
-   return;
+   return (0);
case CNTRL('Z'):
suspend(0);
-   return;
+   return (0);
case CNTRL('C'):
crash();
-   return;
+   return (0);
case CNTRL('D'):
crash();
-   return;
+   return (0);
case ERR:
leave(0);
-   return;
+   return (0);
default:
-   return;
+   return (0);
}
lastch = ch;
if (growing == 0) {
@@ -352,6 +369,7 @@ process(int ch)
wmove(tv, head-y, head-x);
wrefresh(tv);
}
+   return (1);
 }
 
 struct body *



kern_tame.c: fix strncmp call

2015-08-23 Thread Caspar Schutijser
Patch below.

Thanks,
Caspar Schutijser


Index: sys/kern/kern_tame.c
===
RCS file: /cvs/src/sys/kern/kern_tame.c,v
retrieving revision 1.25
diff -u -p -r1.25 kern_tame.c
--- sys/kern/kern_tame.c23 Aug 2015 19:32:20 -  1.25
+++ sys/kern/kern_tame.c23 Aug 2015 21:22:38 -
@@ -423,7 +423,7 @@ tame_namei(struct proc *p, char *origpat
 */
if ((p-p_p-ps_tame  _TM_TMPPATH) 
(p-p_tame_syscall == SYS_unlink) 
-   strncmp(path, /tmp/, sizeof(/tmp) - 1) == 0) {
+   strncmp(path, /tmp/, sizeof(/tmp/) - 1) == 0) {
return (0);
}
 



Re: kern_tame.c: fix strncmp call

2015-08-23 Thread patrick keshishian
On 8/23/15, Caspar Schutijser cas...@schutijser.com wrote:
 Patch below.

 Thanks,
 Caspar Schutijser


 Index: sys/kern/kern_tame.c
 ===
 RCS file: /cvs/src/sys/kern/kern_tame.c,v
 retrieving revision 1.25
 diff -u -p -r1.25 kern_tame.c
 --- sys/kern/kern_tame.c  23 Aug 2015 19:32:20 -  1.25
 +++ sys/kern/kern_tame.c  23 Aug 2015 21:22:38 -
 @@ -423,7 +423,7 @@ tame_namei(struct proc *p, char *origpat
*/
   if ((p-p_p-ps_tame  _TM_TMPPATH) 
   (p-p_tame_syscall == SYS_unlink) 
 - strncmp(path, /tmp/, sizeof(/tmp) - 1) == 0) {
 + strncmp(path, /tmp/, sizeof(/tmp/) - 1) == 0) {

you are confusing sizeof() with strlen(). former counts the byte
required for the terminating NUL.

$ cat /tmp/a.c
#include stdio.h
#include stdlib.h

int main(int argc, char *argv[])
{
printf(sizeof(\/tmp\)=%zu\n, sizeof(/tmp));
exit(0);
}
$ cc  /tmp/a.c -o /tmp/a
$ /tmp/a
sizeof(/tmp)=5

--patrick

   return (0);
   }






Re: kern_tame.c: fix strncmp call

2015-08-23 Thread patrick keshishian
Apologies, my eyes failed me on this.

On 8/23/15, patrick keshishian pkesh...@gmail.com wrote:
 On 8/23/15, Caspar Schutijser cas...@schutijser.com wrote:
 Patch below.

 Thanks,
 Caspar Schutijser


 Index: sys/kern/kern_tame.c
 ===
 RCS file: /cvs/src/sys/kern/kern_tame.c,v
 retrieving revision 1.25
 diff -u -p -r1.25 kern_tame.c
 --- sys/kern/kern_tame.c 23 Aug 2015 19:32:20 -  1.25
 +++ sys/kern/kern_tame.c 23 Aug 2015 21:22:38 -
 @@ -423,7 +423,7 @@ tame_namei(struct proc *p, char *origpat
   */
  if ((p-p_p-ps_tame  _TM_TMPPATH) 
  (p-p_tame_syscall == SYS_unlink) 
 -strncmp(path, /tmp/, sizeof(/tmp) - 1) == 0) {
 +strncmp(path, /tmp/, sizeof(/tmp/) - 1) == 0) {

 you are confusing sizeof() with strlen(). former counts the byte
 required for the terminating NUL.

 $ cat /tmp/a.c
 #include stdio.h
 #include stdlib.h

 int main(int argc, char *argv[])
 {
   printf(sizeof(\/tmp\)=%zu\n, sizeof(/tmp));
   exit(0);
 }
 $ cc  /tmp/a.c -o /tmp/a
 $ /tmp/a
 sizeof(/tmp)=5

 --patrick

  return (0);
  }







restore(8): nanosecond precision

2015-08-23 Thread Philip Guenther

Diff below switches restore(8) to using utimensat() instead of utimes() so 
that file timestamps are restored with full nanosecond precision.  
Eliminates some pointless divisions...

ok?


Index: dirs.c
===
RCS file: /cvs/src/sbin/restore/dirs.c,v
retrieving revision 1.40
diff -u -p -r1.40 dirs.c
--- dirs.c  20 Jan 2015 18:22:21 -  1.40
+++ dirs.c  24 Aug 2015 00:12:48 -
@@ -75,8 +75,8 @@ static struct inotab *inotab[HASHSIZE];
  */
 struct modeinfo {
ino_t ino;
-   struct timeval ctimep[2];
-   struct timeval mtimep[2];
+   struct timespec ctimep[2];
+   struct timespec mtimep[2];
mode_t mode;
uid_t uid;
gid_t gid;
@@ -618,8 +618,8 @@ setdirmodes(int flags)
(void)chown(cp, node.uid, node.gid);
(void)chmod(cp, node.mode);
(void)chflags(cp, node.flags);
-   (void)utimes(cp, node.ctimep);
-   (void)utimes(cp, node.mtimep);
+   (void)utimensat(AT_FDCWD, cp, node.ctimep, 0);
+   (void)utimensat(AT_FDCWD, cp, node.mtimep, 0);
}
ep-e_flags = ~NEW;
}
@@ -696,13 +696,13 @@ allocinotab(FILE *mf, struct context *ct
return (itp);
node.ino = ctxp-ino;
node.mtimep[0].tv_sec = ctxp-atime_sec;
-   node.mtimep[0].tv_usec = ctxp-atime_nsec / 1000;
+   node.mtimep[0].tv_nsec = ctxp-atime_nsec;
node.mtimep[1].tv_sec = ctxp-mtime_sec;
-   node.mtimep[1].tv_usec = ctxp-mtime_nsec / 1000;
+   node.mtimep[1].tv_nsec = ctxp-mtime_nsec;
node.ctimep[0].tv_sec = ctxp-atime_sec;
-   node.ctimep[0].tv_usec = ctxp-atime_nsec / 1000;
+   node.ctimep[0].tv_nsec = ctxp-atime_nsec;
node.ctimep[1].tv_sec = ctxp-birthtime_sec;
-   node.ctimep[1].tv_usec = ctxp-birthtime_nsec / 1000;
+   node.ctimep[1].tv_nsec = ctxp-birthtime_nsec;
node.mode = ctxp-mode;
node.flags = ctxp-file_flags;
node.uid = ctxp-uid;
Index: tape.c
===
RCS file: /cvs/src/sbin/restore/tape.c,v
retrieving revision 1.45
diff -u -p -r1.45 tape.c
--- tape.c  16 Jan 2015 06:40:00 -  1.45
+++ tape.c  24 Aug 2015 00:12:48 -
@@ -505,23 +505,23 @@ extractfile(char *name)
uid_t uid;
gid_t gid;
mode_t mode;
-   struct timeval mtimep[2], ctimep[2];
+   struct timespec mtimep[2], ctimep[2];
struct entry *ep;
int setbirth;
 
curfile.name = name;
curfile.action = USING;
mtimep[0].tv_sec = curfile.atime_sec;
-   mtimep[0].tv_usec = curfile.atime_nsec / 1000;
+   mtimep[0].tv_nsec = curfile.atime_nsec;
mtimep[1].tv_sec = curfile.mtime_sec;
-   mtimep[1].tv_usec = curfile.mtime_nsec / 1000;
+   mtimep[1].tv_nsec = curfile.mtime_nsec;
 
setbirth = curfile.birthtime_sec != 0;
if (setbirth) {
ctimep[0].tv_sec = curfile.atime_sec;
-   ctimep[0].tv_usec = curfile.atime_nsec / 1000;
+   ctimep[0].tv_nsec = curfile.atime_nsec;
ctimep[1].tv_sec = curfile.birthtime_sec;
-   ctimep[1].tv_usec = curfile.birthtime_nsec / 1000;
+   ctimep[1].tv_nsec = curfile.birthtime_nsec;
}
uid = curfile.uid;
gid = curfile.gid;
@@ -583,8 +583,8 @@ extractfile(char *name)
(void)chflags(name, flags);
skipfile();
if (setbirth)
-   (void)utimes(name, ctimep);
-   (void)utimes(name, mtimep);
+   (void)utimensat(AT_FDCWD, name, ctimep, 0);
+   (void)utimensat(AT_FDCWD, name, mtimep, 0);
return (GOOD);
 
case IFIFO:
@@ -603,8 +603,8 @@ extractfile(char *name)
(void)chflags(name, flags);
skipfile();
if (setbirth)
-   (void)utimes(name, ctimep);
-   (void)utimes(name, mtimep);
+   (void)utimensat(AT_FDCWD, name, ctimep, 0);
+   (void)utimensat(AT_FDCWD, name, mtimep, 0);
return (GOOD);
 
case IFREG:
@@ -625,8 +625,8 @@ extractfile(char *name)
getfile(xtrfile, xtrskip);
(void)close(ofile);
if (setbirth)
-   (void)utimes(name, ctimep);
-   (void)utimes(name, mtimep);
+   (void)utimensat(AT_FDCWD, name, ctimep, 0);
+   (void)utimensat(AT_FDCWD, name, mtimep, 0);
return (GOOD);
}
/* NOTREACHED */



some more thinkpad docks support for azalia(4)

2015-08-23 Thread Dmitry Alenichev
Index: azalia.h
===
RCS file: /cvs/src/sys/dev/pci/azalia.h,v
retrieving revision 1.63
diff -u -p -r1.63 azalia.h
--- azalia.h20 Aug 2015 06:44:06 -  1.63
+++ azalia.h24 Aug 2015 00:15:01 -
@@ -512,6 +512,7 @@
 #define AZ_QRK_WID_AD1981_OAMP 0x8000
 #define AZ_QRK_WID_TPDOCK1 0x0001
 #define AZ_QRK_WID_TPDOCK2 0x0002
+#define AZ_QRK_WID_TPDOCK3 0x0003
 
 /* memory-mapped types */
 typedef struct {
Index: azalia_codec.c
===
RCS file: /cvs/src/sys/dev/pci/azalia_codec.c,v
retrieving revision 1.169
diff -u -p -r1.169 azalia_codec.c
--- azalia_codec.c  21 Aug 2015 06:11:04 -  1.169
+++ azalia_codec.c  24 Aug 2015 00:15:01 -
@@ -321,6 +321,27 @@ azalia_codec_init_vtbl(codec_t *this)
case 0x14f15051:
this-name = Conexant CX20561;  /* Hermosa */
break;
+   case 0x14f1506e:
+   this-name = Conexant CX20590;
+   /*
+* Enable dock audio on Thinkpad docks
+* 0x17aa : 0x20f2 = Thinkpad T400
+* 0x17aa : 0x215e = Thinkpad T410
+* 0x17aa : 0x215f = Thinkpad T510
+* 0x17aa : 0x21ce = Thinkpad T420
+* 0x17aa : 0x21cf = Thinkpad T520
+* 0x17aa : 0x21da = Thinkpad X220
+* 0x17aa : 0x21db = Thinkpad X220t
+*/
+   if (this-subid == 0x20f217aa ||
+   this-subid == 0x215e17aa ||
+   this-subid == 0x215f17aa ||
+   this-subid == 0x21ce17aa ||
+   this-subid == 0x21cf17aa ||
+   this-subid == 0x21da17aa ||
+   this-subid == 0x21db17aa)
+   this-qrks |= AZ_QRK_WID_TPDOCK3;
+   break;
case 0x434d4980:
this-name = CMedia CMI9880;
break;
@@ -2537,6 +2558,20 @@ azalia_codec_widget_quirks(codec_t *this
nid == 0x19) {
/* Thinkpad x240/t440 style dock microphone */
w-d.pin.config = 0x21a11010;
+   w-enable = 1;
+   }
+
+   if (this-qrks  AZ_QRK_WID_TPDOCK3 
+   nid == 0x1a) {
+   /* Thinkpad x220/t420 style dock microphone */
+   w-d.pin.config = 0x21a190f0;
+   w-enable = 1;
+   }
+
+   if (this-qrks  AZ_QRK_WID_TPDOCK3 
+   nid == 0x1c) {
+   /* Thinkpad x220/t420 style dock headphone */
+   w-d.pin.config = 0x212140ff;
w-enable = 1;
}
 



Re: kern_tame.c: fix strncmp call

2015-08-23 Thread Alexander Hall

On 08/24/15 00:29, patrick keshishian wrote:

On 8/23/15, Caspar Schutijser cas...@schutijser.com wrote:

Patch below.

Thanks,
Caspar Schutijser


Index: sys/kern/kern_tame.c
===
RCS file: /cvs/src/sys/kern/kern_tame.c,v
retrieving revision 1.25
diff -u -p -r1.25 kern_tame.c
--- sys/kern/kern_tame.c23 Aug 2015 19:32:20 -  1.25
+++ sys/kern/kern_tame.c23 Aug 2015 21:22:38 -
@@ -423,7 +423,7 @@ tame_namei(struct proc *p, char *origpat
 */
if ((p-p_p-ps_tame  _TM_TMPPATH) 
(p-p_tame_syscall == SYS_unlink) 
-   strncmp(path, /tmp/, sizeof(/tmp) - 1) == 0) {
+   strncmp(path, /tmp/, sizeof(/tmp/) - 1) == 0) {


you are confusing sizeof() with strlen(). former counts the byte
required for the terminating NUL.


Yes, but you're missing the following - 1, methinks...

I think the diff is correct.

/Alexander



$ cat /tmp/a.c
#include stdio.h
#include stdlib.h

int main(int argc, char *argv[])
{
printf(sizeof(\/tmp\)=%zu\n, sizeof(/tmp));
exit(0);
}
$ cc  /tmp/a.c -o /tmp/a
$ /tmp/a
sizeof(/tmp)=5

--patrick


return (0);
}









Re: worm(6) remove cheating bug

2015-08-23 Thread Rafael Zalamena
On Sun, Aug 23, 2015 at 06:07:46PM -0300, Rafael Zalamena wrote:
 I just fixed a bug which allowed people to cheat in worm(6). This bug was
 found out by deraadt@ when peer reviewing the mail thread in tech@
 'Fwd: worm.c removing unused variables'.
 
 To reproduce the bug simply hold spacebar and your worm won't move.
 
 Highlights:
  * Use the unused time variables to record how long has been since the
last valid key press;
  * Changed the process() function to return whether the key pressed was
valid or not;
  * Use clock_gettime(CLOCK_UPTIME) instead of gettimeofday() to provide
reliable clock source even on suspend/wake up;
 

More feedback from deraadt@ and guenter@.

Changes:
 * Replaced memset() with timerspecclear();
 * Fixed the time out comment before clock_gettime();
 * Replaced poll() with ppoll() to use timespec directly instead of math
   operations;

ok?

Index: worm.c
===
RCS file: /cvs/src/games/worm/worm.c,v
retrieving revision 1.30
diff -u -p -r1.30 worm.c
--- worm.c  22 Aug 2015 14:47:41 -  1.30
+++ worm.c  24 Aug 2015 02:51:09 -
@@ -78,7 +78,7 @@ void  leave(int);
 void   life(void);
 void   newpos(struct body *);
 struct body*newlink(void);
-void   process(int);
+intprocess(int);
 void   prize(void);
 intrnd(int);
 void   setup(void);
@@ -88,10 +88,11 @@ int
 main(int argc, char **argv)
 {
int retval;
-   struct timeval t, tod;
-   struct timezone tz;
struct pollfd pfd[1];
const char *errstr;
+   struct timespec t, tn, tdiff;
+
+   timespecclear(t);
 
setbuf(stdout, outbuf);
signal(SIGINT, leave);
@@ -154,18 +155,33 @@ main(int argc, char **argv)
running--;
process(lastch);
} else {
-   /* fflush(stdout); */
-   /* Delay could be a command line option */
-   t.tv_sec = 1;
-   t.tv_usec = 0;
-   (void)gettimeofday(tod, tz);
+   /* Check for timeout. */
+   clock_gettime(CLOCK_UPTIME, tn);
+   if (timespeccmp(t, tn, =)) {
+   t = tn;
+   t.tv_sec += 1;
+
+   process(lastch);
+   continue;
+   }
+
+   /* Prepare next read */
pfd[0].fd = STDIN_FILENO;
pfd[0].events = POLLIN;
-   retval = poll(pfd, 1, t.tv_sec * 1000 + t.tv_usec / 
1000);
-   if (retval  0)
-   process(getch());
-   else
-   process(lastch);
+   timespecsub(t, tn, tdiff);
+   retval = ppoll(pfd, 1, tdiff, NULL);
+
+   /* Nothing to do if timed out or signal. */
+   if (retval = 0)
+   continue;
+
+   /* Only update timer if valid key was pressed. */
+   if (process(getch()) == 0)
+   continue;
+
+   /* Update using clock_gettime(), tn is too old now. */
+   clock_gettime(CLOCK_UPTIME, t);
+   t.tv_sec += 1;
}
}
 }
@@ -245,7 +261,7 @@ prize(void)
wrefresh(tv);
 }
 
-void
+int
 process(int ch)
 {
int x,y;
@@ -300,21 +316,21 @@ process(int ch)
break;
case '\f':
setup();
-   return;
+   return (0);
case CNTRL('Z'):
suspend(0);
-   return;
+   return (0);
case CNTRL('C'):
crash();
-   return;
+   return (0);
case CNTRL('D'):
crash();
-   return;
+   return (0);
case ERR:
leave(0);
-   return;
+   return (0);
default:
-   return;
+   return (0);
}
lastch = ch;
if (growing == 0) {
@@ -352,6 +368,7 @@ process(int ch)
wmove(tv, head-y, head-x);
wrefresh(tv);
}
+   return (1);
 }
 
 struct body *



Re: some more thinkpad docks support for azalia(4)

2015-08-23 Thread Jonathan Gray
On Mon, Aug 24, 2015 at 03:15:51AM +0300, Dmitry Alenichev wrote:
 Index: azalia.h
 ===
 RCS file: /cvs/src/sys/dev/pci/azalia.h,v
 retrieving revision 1.63
 diff -u -p -r1.63 azalia.h
 --- azalia.h  20 Aug 2015 06:44:06 -  1.63
 +++ azalia.h  24 Aug 2015 00:15:01 -
 @@ -512,6 +512,7 @@
  #define AZ_QRK_WID_AD1981_OAMP   0x8000
  #define AZ_QRK_WID_TPDOCK1   0x0001
  #define AZ_QRK_WID_TPDOCK2   0x0002
 +#define AZ_QRK_WID_TPDOCK3   0x0003

This should be 0x0004 (1  18)

0x0003 is (1  17) and (1  16) effectively setting both
AZ_QRK_WID_TPDOCK1 and AZ_QRK_WID_TPDOCK2.

The rest of the diff looks fine.

  
  /* memory-mapped types */
  typedef struct {
 Index: azalia_codec.c
 ===
 RCS file: /cvs/src/sys/dev/pci/azalia_codec.c,v
 retrieving revision 1.169
 diff -u -p -r1.169 azalia_codec.c
 --- azalia_codec.c21 Aug 2015 06:11:04 -  1.169
 +++ azalia_codec.c24 Aug 2015 00:15:01 -
 @@ -321,6 +321,27 @@ azalia_codec_init_vtbl(codec_t *this)
   case 0x14f15051:
   this-name = Conexant CX20561;  /* Hermosa */
   break;
 + case 0x14f1506e:
 + this-name = Conexant CX20590;
 + /*
 +  * Enable dock audio on Thinkpad docks
 +  * 0x17aa : 0x20f2 = Thinkpad T400
 +  * 0x17aa : 0x215e = Thinkpad T410
 +  * 0x17aa : 0x215f = Thinkpad T510
 +  * 0x17aa : 0x21ce = Thinkpad T420
 +  * 0x17aa : 0x21cf = Thinkpad T520
 +  * 0x17aa : 0x21da = Thinkpad X220
 +  * 0x17aa : 0x21db = Thinkpad X220t
 +  */
 + if (this-subid == 0x20f217aa ||
 + this-subid == 0x215e17aa ||
 + this-subid == 0x215f17aa ||
 + this-subid == 0x21ce17aa ||
 + this-subid == 0x21cf17aa ||
 + this-subid == 0x21da17aa ||
 + this-subid == 0x21db17aa)
 + this-qrks |= AZ_QRK_WID_TPDOCK3;
 + break;
   case 0x434d4980:
   this-name = CMedia CMI9880;
   break;
 @@ -2537,6 +2558,20 @@ azalia_codec_widget_quirks(codec_t *this
   nid == 0x19) {
   /* Thinkpad x240/t440 style dock microphone */
   w-d.pin.config = 0x21a11010;
 + w-enable = 1;
 + }
 +
 + if (this-qrks  AZ_QRK_WID_TPDOCK3 
 + nid == 0x1a) {
 + /* Thinkpad x220/t420 style dock microphone */
 + w-d.pin.config = 0x21a190f0;
 + w-enable = 1;
 + }
 +
 + if (this-qrks  AZ_QRK_WID_TPDOCK3 
 + nid == 0x1c) {
 + /* Thinkpad x220/t420 style dock headphone */
 + w-d.pin.config = 0x212140ff;
   w-enable = 1;
   }
  
 



Re: kern_tame.c: fix strncmp call

2015-08-23 Thread Joerg Sonnenberger
On Sun, Aug 23, 2015 at 03:29:46PM -0700, patrick keshishian wrote:
 On 8/23/15, Caspar Schutijser cas...@schutijser.com wrote:
  Patch below.
 
  Thanks,
  Caspar Schutijser
 
 
  Index: sys/kern/kern_tame.c
  ===
  RCS file: /cvs/src/sys/kern/kern_tame.c,v
  retrieving revision 1.25
  diff -u -p -r1.25 kern_tame.c
  --- sys/kern/kern_tame.c23 Aug 2015 19:32:20 -  1.25
  +++ sys/kern/kern_tame.c23 Aug 2015 21:22:38 -
  @@ -423,7 +423,7 @@ tame_namei(struct proc *p, char *origpat
   */
  if ((p-p_p-ps_tame  _TM_TMPPATH) 
  (p-p_tame_syscall == SYS_unlink) 
  -   strncmp(path, /tmp/, sizeof(/tmp) - 1) == 0) {
  +   strncmp(path, /tmp/, sizeof(/tmp/) - 1) == 0) {
 
 you are confusing sizeof() with strlen(). former counts the byte
 required for the terminating NUL.

I don't think the OP is. If you want to check that path starts with
/tmp/, you need to check the first 5 characters. The original code
only checks the first 4. As such, it will also match /tmpfile.

Joerg