• # Avancée notable

    Posté par (site web personnel) . En réponse au journal Tame et OpenBSD. Évalué à 10.

    Une petit exemple vaut mieux qu'un long discours

    Implémentation sans raffinage de Tame dans uniq

    Index: usr.bin/uniq/uniq.c
    ===================================================================
    RCS file: /cvs/src/usr.bin/uniq/uniq.c,v
    retrieving revision 1.19
    diff -u -p -u -r1.19 uniq.c
    --- usr.bin/uniq/uniq.c 26 Nov 2013 19:25:39 -0000 1.19
    +++ usr.bin/uniq/uniq.c 3 Jun 2015 22:09:03 -0000
    @@ -33,6 +33,7 @@
     * SUCH DAMAGE.
     */
    +#include <sys/tame.h>
     #include <ctype.h>
     #include <err.h>
     #include <errno.h>
    @@ -61,6 +62,8 @@ main(int argc, char *argv[])
     int ch;
     char *prevline, *thisline;
    + tame(TAME_STDIO | TAME_RPATH | TAME_WPATH);
    +
     obsolete(argv);
     while ((ch = getopt(argc, argv, "cdf:s:u")) != -1) {
     const char *errstr;
    @@ -118,6 +121,8 @@ main(int argc, char *argv[])
     default:
     usage();
     }
    +
    + tame(TAME_STDIO);
     prevline = malloc(MAXLINELEN);
     thisline = malloc(MAXLINELEN);

    Implémentation (sans raffinage) de Capsicum dans uniq

    --- head/usr.bin/uniq/uniq.c 2011年11月06日 08:18:11 227193
    +++ head/usr.bin/uniq/uniq.c 2013年07月18日 22:11:27 253457
    @@ -44,15 +44,20 @@
     "$FreeBSD$";
     #endif /* not lint */
    +#include <sys/capability.h>
    +
     #include <ctype.h>
     #include <err.h>
    +#include <errno.h>
     #include <limits.h>
     #include <locale.h>
    +#include <nl_types.h>
     #include <stdint.h>
     #define _WITH_GETLINE
     #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
    +#include <termios.h>
     #include <unistd.h>
     #include <wchar.h>
     #include <wctype.h>
    @@ -68,6 +73,17 @@
     static void obsolete(char *[]);
     static void usage(void);
    +static void
    +strerror_init(void)
    +{
    +
    + /*
    + * Cache NLS data before entering capability mode.
    + * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
    + */
    + (void)catopen("libc", NL_CAT_LOCALE);
    +}
    +
     int
     main (int argc, char *argv[])
     {
    @@ -77,6 +93,7 @@
     size_t prevbuflen, thisbuflen, b1;
     char *prevline, *thisline, *p;
     const char *ifn;
    + cap_rights_t rights;
     (void) setlocale(LC_ALL, "");
    @@ -128,8 +145,34 @@
     ofp = stdout;
     if (argc > 0 && strcmp(argv[0], "-") != 0)
     ifp = file(ifn = argv[0], "r");
    + if (cap_rights_limit(fileno(ifp), CAP_FSTAT | CAP_READ) < 0 &&
    + errno != ENOSYS) {
    + err(1, "unable to limit rights for %s", ifn);
    + }
    + rights = CAP_FSTAT | CAP_WRITE;
     if (argc > 1)
     ofp = file(argv[1], "w");
    + else
    + rights |= CAP_IOCTL;
    + if (cap_rights_limit(fileno(ofp), rights) < 0 && errno != ENOSYS) {
    + err(1, "unable to limit rights for %s",
    + argc > 1 ? argv[1] : "stdout");
    + }
    + if ((rights & CAP_IOCTL) != 0) {
    + unsigned long cmd;
    +
    + cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
    +
    + if (cap_ioctls_limit(fileno(ofp), &cmd, 1) < 0 &&
    + errno != ENOSYS) {
    + err(1, "unable to limit ioctls for %s",
    + argc > 1 ? argv[1] : "stdout");
    + }
    + }
    +
    + strerror_init();
    + if (cap_enter() < 0 && errno != ENOSYS)
    + err(1, "unable to enter capability mode");
     prevbuflen = thisbuflen = 0;
     prevline = thisline = NULL;

    C'est sûr que les possibilités ne sont pas les mêmes, mais le travail pour une couverture complète du système est loin d'être le même non plus.