Re: pledge for tetris
Now that tedu moved the score file to $HOME, we can get by with "stdio rpath wpath cpath tty". Do some basic clean up in the manual. There's more to do, but leave that for later. Index: games/tetris/tetris.6 === RCS file: /var/cvs/src/games/tetris/tetris.6,v retrieving revision 1.16 diff -u -p -r1.16 tetris.6 --- games/tetris/tetris.6 27 Jul 2015 18:48:04 - 1.16 +++ games/tetris/tetris.6 19 Nov 2015 00:19:47 - @@ -146,8 +146,8 @@ Scores which are the highest on a given are marked with asterisks .Dq * . .Sh FILES -.Bl -tag -width /var/games/tetris.scoresxx -.It Pa /var/games/tetris.scores +.Bl -tag -width $HOME/tetris.scores +.It Pa $HOME/tetris.scores high score file .El .Sh AUTHORS Index: games/tetris/tetris.c === RCS file: /var/cvs/src/games/tetris/tetris.c,v retrieving revision 1.25 diff -u -p -r1.25 tetris.c --- games/tetris/tetris.c 16 Nov 2014 04:49:49 - 1.25 +++ games/tetris/tetris.c 19 Nov 2015 00:10:01 - @@ -61,7 +61,6 @@ const struct shape *curshape; const struct shape *nextshape; long fallrate; intscore; -gid_t gid, egid; char key_msg[100]; intshowpreview, classic; @@ -157,11 +156,10 @@ main(int argc, char *argv[]) const char *errstr; int ch, i, j; - keys = "jkl pq"; + if (pledge("stdio rpath wpath cpath tty", NULL) == -1) + err(1, "pledge"); - gid = getgid(); - egid = getegid(); - setegid(gid); + keys = "jkl pq"; classic = showpreview = 0; while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) Index: games/tetris/tetris.h === RCS file: /var/cvs/src/games/tetris/tetris.h,v retrieving revision 1.10 diff -u -p -r1.10 tetris.h --- games/tetris/tetris.h 10 Aug 2008 12:23:25 - 1.10 +++ games/tetris/tetris.h 19 Nov 2015 00:05:11 - @@ -167,7 +167,6 @@ extern long fallrate; /* less than 1 mil #define PRE_PENALTY 0.75 extern int score; /* the obvious thing */ -extern gid_t gid, egid; extern charkey_msg[100]; extern int showpreview;
Re: pledge for tetris
On Tue, Nov 17, 2015 at 12:21:32AM -0500, Michael McConville wrote: Ted Unangst wrote: Theo Buehler wrote: > drop the score file support No way! this is critical functionality. :) Seconded. :P I disagree - the obvious solution is to move tetris to ports for anyone who needs score file support, and for Ted to write a new cut down alternative version to go in base. -- Tati Chevron Perl and FORTRAN specialist. SWABSIT development and migration department. http://www.swabsit.com
Re: pledge for tetris
Ted Unangst wrote: > Theo Buehler wrote: > > In its current form, tetris is a setgid program and needs a whopping > > > > pledge("stdio rpath wpath cpath flock getpw id tty") > > > > throughout its lifetime because of the score file in /var/games. > > > > As discussed with Theo off-list, this is risk-only. Thus, drop the > > score file support, lose the setgid bit and make tetris a much more > > reasonable pledge("stdio rpath tty") program for relaxed game play. > > No way! this is critical functionality. :) > > Can you just change it to save the scores in HOME/.tetrisscores? The score code still makes a weak attempt to save unique users, which i may relax, but this should let you get by with just basic stdio stuff. Index: Makefile === RCS file: /cvs/src/games/tetris/Makefile,v retrieving revision 1.7 diff -u -p -r1.7 Makefile --- Makefile31 May 2002 03:46:35 - 1.7 +++ Makefile17 Nov 2015 05:35:40 - @@ -5,14 +5,5 @@ SRCS= input.c screen.c shapes.c scores.c MAN= tetris.6 DPADD= ${LIBCURSES} LDADD= -lcurses -BINMODE=2555 - -beforeinstall: - @if [ ! -f ${DESTDIR}/var/games/tetris.scores ]; then \ - ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m 664 \ - /dev/null ${DESTDIR}/var/games/tetris.scores ; \ - else \ - true ; \ - fi .include Index: pathnames.h === RCS file: pathnames.h diff -N pathnames.h --- pathnames.h 3 Jun 2003 03:01:41 - 1.3 +++ /dev/null 1 Jan 1970 00:00:00 - @@ -1,38 +0,0 @@ -/* $OpenBSD: pathnames.h,v 1.3 2003/06/03 03:01:41 millert Exp $ */ -/* $NetBSD: pathnames.h,v 1.2 1995/04/22 07:42:37 cgd Exp $*/ - -/*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek and Darren F. Provine. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - *notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - *notice, this list of conditions and the following disclaimer in the - *documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - *may be used to endorse or promote products derived from this software - *without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * @(#)pathnames.h 8.1 (Berkeley) 5/31/93 - */ - -#define _PATH_SCOREFILE"/var/games/tetris.scores" Index: scores.c === RCS file: /cvs/src/games/tetris/scores.c,v retrieving revision 1.12 diff -u -p -r1.12 scores.c --- scores.c16 Nov 2014 04:49:49 - 1.12 +++ scores.c17 Nov 2015 05:43:50 - @@ -49,7 +49,6 @@ #include #include #include -#include #include #include #include @@ -57,7 +56,6 @@ #include #include -#include "pathnames.h" #include "screen.h" #include "scores.h" #include "tetris.h" @@ -98,50 +96,44 @@ getscores(FILE **fpp) { int sd, mint, lck, mask, i; char *mstr, *human; + char scorepath[PATH_MAX]; FILE *sf; if (fpp != NULL) { - mint = O_RDWR | O_CREAT; + mint = O_RDWR | O_CREAT | O_EXLOCK; mstr = "r+"; human = "read/write"; - lck = LOCK_EX; + *fpp = NULL; } else { - mint = O_RDONLY; + mint = O_RDONLY | O_EXLOCK; mstr = "r"; human = "reading"; - lck = LOCK_SH; } - setegid(egid); + if (!getenv("HOME")) + return; mask = umask(S_IWOTH); - sd = open(_PATH_SCOREFILE, mint, 0666); + snprintf(scorepath, sizeof(scorepath), "%s/%s", getenv("HOME"), ".tet
Re: pledge for tetris
On Tue, Nov 17, 2015 at 12:15:59AM -0500, Ted Unangst wrote: > Theo Buehler wrote: > > In its current form, tetris is a setgid program and needs a whopping > > > > pledge("stdio rpath wpath cpath flock getpw id tty") > > > > throughout its lifetime because of the score file in /var/games. > > > > As discussed with Theo off-list, this is risk-only. Thus, drop the > > score file support, lose the setgid bit and make tetris a much more > > reasonable pledge("stdio rpath tty") program for relaxed game play. > > No way! this is critical functionality. :) > > Can you just change it to save the scores in HOME/.tetrisscores? I thought about that, but I'm not a fan. This would still mean "stdio rpath wpath cpath flock tty" and "getpw" if we want to keep the current file format.
Re: pledge for tetris
Ted Unangst wrote: > Theo Buehler wrote: > > drop the score file support > > No way! this is critical functionality. :) Seconded. :P
Re: pledge for tetris
Theo Buehler wrote: > In its current form, tetris is a setgid program and needs a whopping > > pledge("stdio rpath wpath cpath flock getpw id tty") > > throughout its lifetime because of the score file in /var/games. > > As discussed with Theo off-list, this is risk-only. Thus, drop the > score file support, lose the setgid bit and make tetris a much more > reasonable pledge("stdio rpath tty") program for relaxed game play. No way! this is critical functionality. :) Can you just change it to save the scores in HOME/.tetrisscores?
Re: pledge for tetris
In its current form, tetris is a setgid program and needs a whopping pledge("stdio rpath wpath cpath flock getpw id tty") throughout its lifetime because of the score file in /var/games. As discussed with Theo off-list, this is risk-only. Thus, drop the score file support, lose the setgid bit and make tetris a much more reasonable pledge("stdio rpath tty") program for relaxed game play. Index: games/tetris/Makefile === RCS file: /cvs/src/games/tetris/Makefile,v retrieving revision 1.7 diff -u -p -r1.7 Makefile --- games/tetris/Makefile 31 May 2002 03:46:35 - 1.7 +++ games/tetris/Makefile 17 Nov 2015 04:50:55 - @@ -1,18 +1,9 @@ # $OpenBSD: Makefile,v 1.7 2002/05/31 03:46:35 pjanzen Exp $ PROG= tetris -SRCS= input.c screen.c shapes.c scores.c tetris.c +SRCS= input.c screen.c shapes.c tetris.c MAN= tetris.6 DPADD= ${LIBCURSES} LDADD= -lcurses -BINMODE=2555 - -beforeinstall: - @if [ ! -f ${DESTDIR}/var/games/tetris.scores ]; then \ - ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m 664 \ - /dev/null ${DESTDIR}/var/games/tetris.scores ; \ - else \ - true ; \ - fi .include Index: games/tetris/pathnames.h === RCS file: games/tetris/pathnames.h diff -N games/tetris/pathnames.h --- games/tetris/pathnames.h3 Jun 2003 03:01:41 - 1.3 +++ /dev/null 1 Jan 1970 00:00:00 - @@ -1,38 +0,0 @@ -/* $OpenBSD: pathnames.h,v 1.3 2003/06/03 03:01:41 millert Exp $ */ -/* $NetBSD: pathnames.h,v 1.2 1995/04/22 07:42:37 cgd Exp $*/ - -/*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek and Darren F. Provine. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - *notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - *notice, this list of conditions and the following disclaimer in the - *documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - *may be used to endorse or promote products derived from this software - *without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * @(#)pathnames.h 8.1 (Berkeley) 5/31/93 - */ - -#define _PATH_SCOREFILE"/var/games/tetris.scores" Index: games/tetris/scores.c === RCS file: games/tetris/scores.c diff -N games/tetris/scores.c --- games/tetris/scores.c 16 Nov 2014 04:49:49 - 1.12 +++ /dev/null 1 Jan 1970 00:00:00 - @@ -1,444 +0,0 @@ -/* $OpenBSD: scores.c,v 1.12 2014/11/16 04:49:49 guenther Exp $*/ -/* $NetBSD: scores.c,v 1.2 1995/04/22 07:42:38 cgd Exp $ */ - -/*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek and Darren F. Provine. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - *notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - *notice, this list of conditions and the following disclaimer in the - *documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - *may be used to endorse or promote products derived from this software - *without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS `
Re: pledge for tetris
Please disregard. This patch breaks multiple simultaneous games. Sorry for the noise.
pledge for tetris
pledge("stdio rpath wpath cpath flock getpw id tty") for setup then play the game with "stdio rpath wpath getpw tty". Most of the promises are needed for dealing with the score file. The function getscores() currently appears towards the end of the program's lifetime and it calls setegid() and flock(). By hoisting the latest possible call to getscores() to right after option parsing, we can drop the most powerful promises before starting game play, at the cost of refactoring a bit. The "getpw" promise is for an unlikely error path in thisuser() that might be called very late. Index: games/tetris/scores.c === RCS file: /cvs/src/games/tetris/scores.c,v retrieving revision 1.12 diff -u -p -r1.12 scores.c --- games/tetris/scores.c 16 Nov 2014 04:49:49 - 1.12 +++ games/tetris/scores.c 15 Nov 2015 12:28:17 - @@ -73,14 +73,13 @@ #define NUMSPOTS (MAXHISCORES + 1) #defineNLEVELS (MAXLEVEL + 1) +extern int gotscores; static time_t now; static int nscores; -static int gotscores; static struct highscore scores[NUMSPOTS]; static int checkscores(struct highscore *, int); static int cmpscores(const void *, const void *); -static void getscores(FILE **); static void printem(int, int, struct highscore *, int, const char *); static char *thisuser(void); @@ -93,7 +92,7 @@ static char *thisuser(void); * * Note, we assume closing the stdio file releases the lock. */ -static void +void getscores(FILE **fpp) { int sd, mint, lck, mask, i; @@ -150,16 +149,13 @@ getscores(FILE **fpp) } void -savescore(int level) +savescore(FILE *sf, int level) { struct highscore *sp; int i; int change; - FILE *sf; const char *me; - getscores(&sf); - gotscores = 1; (void)time(&now); /* Index: games/tetris/scores.h === RCS file: /cvs/src/games/tetris/scores.h,v retrieving revision 1.6 diff -u -p -r1.6 scores.h --- games/tetris/scores.h 16 Nov 2014 04:49:49 - 1.6 +++ games/tetris/scores.h 15 Nov 2015 12:28:17 - @@ -49,5 +49,6 @@ struct highscore { #define MAXSCORES 9 /* maximum high score entries per person */ #defineEXPIRATION (5L * 365 * 24 * 60 * 60) -void savescore(int); +void getscores(FILE **); +void savescore(FILE *, int); void showscores(int); Index: games/tetris/tetris.c === RCS file: /cvs/src/games/tetris/tetris.c,v retrieving revision 1.25 diff -u -p -r1.25 tetris.c --- games/tetris/tetris.c 16 Nov 2014 04:49:49 - 1.25 +++ games/tetris/tetris.c 15 Nov 2015 12:28:17 - @@ -60,6 +60,7 @@ int Rows, Cols; const struct shape *curshape; const struct shape *nextshape; long fallrate; +intgotscores; intscore; gid_t gid, egid; char key_msg[100]; @@ -150,6 +151,7 @@ randshape(void) int main(int argc, char *argv[]) { + FILE *sf; int pos, c; char *keys; int level = 2; @@ -159,6 +161,9 @@ main(int argc, char *argv[]) keys = "jkl pq"; + if (pledge("stdio rpath wpath cpath flock getpw id tty", NULL) == -1) + err(1, "pledge"); + gid = getgid(); egid = getegid(); setegid(gid); @@ -201,6 +206,12 @@ main(int argc, char *argv[]) if (argc) usage(); + getscores(&sf); + gotscores = 1; + + if (pledge("stdio rpath wpath getpw tty", NULL) == -1) + err(1, "pledge"); + fallrate = 100 / level; for (i = 0; i <= 5; i++) { @@ -337,7 +348,7 @@ main(int argc, char *argv[]) (int)(score * level * PRE_PENALTY)); score = score * PRE_PENALTY; } - savescore(level); + savescore(sf, level); printf("\nHit RETURN to see high scores, ^C to skip.\n");