1*d0f245d2SMark Johnston /*- 2*d0f245d2SMark Johnston * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*d0f245d2SMark Johnston * 4*d0f245d2SMark Johnston * Copyright (c) 2022 The FreeBSD Foundation 5*d0f245d2SMark Johnston * 6*d0f245d2SMark Johnston * This software was developed by Mark Johnston under sponsorship from 7*d0f245d2SMark Johnston * the FreeBSD Foundation. 8*d0f245d2SMark Johnston * 9*d0f245d2SMark Johnston * Redistribution and use in source and binary forms, with or without 10*d0f245d2SMark Johnston * modification, are permitted provided that the following conditions are 11*d0f245d2SMark Johnston * met: 12*d0f245d2SMark Johnston * 1. Redistributions of source code must retain the above copyright 13*d0f245d2SMark Johnston * notice, this list of conditions and the following disclaimer. 14*d0f245d2SMark Johnston * 2. Redistributions in binary form must reproduce the above copyright 15*d0f245d2SMark Johnston * notice, this list of conditions and the following disclaimer in 16*d0f245d2SMark Johnston * the documentation and/or other materials provided with the distribution. 17*d0f245d2SMark Johnston * 18*d0f245d2SMark Johnston * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19*d0f245d2SMark Johnston * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*d0f245d2SMark Johnston * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*d0f245d2SMark Johnston * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22*d0f245d2SMark Johnston * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23*d0f245d2SMark Johnston * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24*d0f245d2SMark Johnston * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25*d0f245d2SMark Johnston * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26*d0f245d2SMark Johnston * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27*d0f245d2SMark Johnston * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28*d0f245d2SMark Johnston * SUCH DAMAGE. 29*d0f245d2SMark Johnston */ 30*d0f245d2SMark Johnston 31*d0f245d2SMark Johnston #include <sys/capsicum.h> 32*d0f245d2SMark Johnston #include <stdio.h> 33*d0f245d2SMark Johnston #include <stdlib.h> 34*d0f245d2SMark Johnston 35*d0f245d2SMark Johnston #include <atf-c.h> 36*d0f245d2SMark Johnston #include <sysdecode.h> 37*d0f245d2SMark Johnston 38*d0f245d2SMark Johnston /* 39*d0f245d2SMark Johnston * Take a comma-separated list of capability rights and verify that all rights 40*d0f245d2SMark Johnston * are present in the specified table, and that all rights in the table are 41*d0f245d2SMark Johnston * present in the list. 42*d0f245d2SMark Johnston */ 43*d0f245d2SMark Johnston static void 44*d0f245d2SMark Johnston check_sysdecode_cap_rights(FILE *fp, char **bufp, size_t *szp, 45*d0f245d2SMark Johnston cap_rights_t *rightsp, const char *tab[]) 46*d0f245d2SMark Johnston { 47*d0f245d2SMark Johnston const char *next, *tok; 48*d0f245d2SMark Johnston char *buf; 49*d0f245d2SMark Johnston int i; 50*d0f245d2SMark Johnston 51*d0f245d2SMark Johnston sysdecode_cap_rights(fp, rightsp); 52*d0f245d2SMark Johnston 53*d0f245d2SMark Johnston ATF_REQUIRE(fflush(fp) == 0); 54*d0f245d2SMark Johnston (*bufp)[*szp] = '\0'; 55*d0f245d2SMark Johnston 56*d0f245d2SMark Johnston buf = strdup(*bufp); 57*d0f245d2SMark Johnston for (tok = buf; (next = strsep(&buf, ",")), tok != NULL; tok = next) { 58*d0f245d2SMark Johnston for (i = 0; tab[i] != NULL; i++) { 59*d0f245d2SMark Johnston if (strcmp(tok, tab[i]) == 0) 60*d0f245d2SMark Johnston break; 61*d0f245d2SMark Johnston } 62*d0f245d2SMark Johnston ATF_REQUIRE_MSG(tab[i] != NULL, 63*d0f245d2SMark Johnston "did not find '%s' in table", tok); 64*d0f245d2SMark Johnston } 65*d0f245d2SMark Johnston free(buf); 66*d0f245d2SMark Johnston 67*d0f245d2SMark Johnston for (i = 0; tab[i] != NULL; i++) { 68*d0f245d2SMark Johnston buf = strdup(*bufp); 69*d0f245d2SMark Johnston for (tok = buf; (next = strsep(&buf, ",")), tok != NULL; 70*d0f245d2SMark Johnston tok = next) { 71*d0f245d2SMark Johnston if (strcmp(tok, tab[i]) == 0) 72*d0f245d2SMark Johnston break; 73*d0f245d2SMark Johnston } 74*d0f245d2SMark Johnston free(buf); 75*d0f245d2SMark Johnston ATF_REQUIRE_MSG(tok != NULL, 76*d0f245d2SMark Johnston "did not find '%s' in output stream", tab[i]); 77*d0f245d2SMark Johnston } 78*d0f245d2SMark Johnston 79*d0f245d2SMark Johnston ATF_REQUIRE(fseek(fp, 0, SEEK_SET) == 0); 80*d0f245d2SMark Johnston } 81*d0f245d2SMark Johnston 82*d0f245d2SMark Johnston /* 83*d0f245d2SMark Johnston * Regression tests for sysdecode_cap_rights(3). 84*d0f245d2SMark Johnston */ 85*d0f245d2SMark Johnston ATF_TC_WITHOUT_HEAD(cap_rights); 86*d0f245d2SMark Johnston ATF_TC_BODY(cap_rights, tc) 87*d0f245d2SMark Johnston { 88*d0f245d2SMark Johnston char *buf; 89*d0f245d2SMark Johnston FILE *fp; 90*d0f245d2SMark Johnston size_t sz; 91*d0f245d2SMark Johnston cap_rights_t rights; 92*d0f245d2SMark Johnston 93*d0f245d2SMark Johnston fp = open_memstream(&buf, &sz); 94*d0f245d2SMark Johnston ATF_REQUIRE(fp != NULL); 95*d0f245d2SMark Johnston 96*d0f245d2SMark Johnston /* 97*d0f245d2SMark Johnston * libsysdecode emits a pseudo-right, CAP_NONE, when no rights are 98*d0f245d2SMark Johnston * present. 99*d0f245d2SMark Johnston */ 100*d0f245d2SMark Johnston check_sysdecode_cap_rights(fp, &buf, &sz, 101*d0f245d2SMark Johnston cap_rights_init(&rights), 102*d0f245d2SMark Johnston (const char *[]){ "CAP_NONE", NULL, }); 103*d0f245d2SMark Johnston 104*d0f245d2SMark Johnston check_sysdecode_cap_rights(fp, &buf, &sz, 105*d0f245d2SMark Johnston cap_rights_init(&rights, CAP_READ, CAP_SEEK), 106*d0f245d2SMark Johnston (const char *[]){ "CAP_PREAD", NULL, }); 107*d0f245d2SMark Johnston 108*d0f245d2SMark Johnston check_sysdecode_cap_rights(fp, &buf, &sz, 109*d0f245d2SMark Johnston cap_rights_init(&rights, CAP_READ, CAP_MMAP, CAP_SEEK_TELL), 110*d0f245d2SMark Johnston (const char *[]){ "CAP_READ", "CAP_MMAP", "CAP_SEEK_TELL", NULL, }); 111*d0f245d2SMark Johnston 112*d0f245d2SMark Johnston check_sysdecode_cap_rights(fp, &buf, &sz, 113*d0f245d2SMark Johnston cap_rights_init(&rights, CAP_MMAP, CAP_READ, CAP_WRITE, CAP_SEEK), 114*d0f245d2SMark Johnston (const char *[]){ "CAP_MMAP_RW", NULL, }); 115*d0f245d2SMark Johnston 116*d0f245d2SMark Johnston check_sysdecode_cap_rights(fp, &buf, &sz, 117*d0f245d2SMark Johnston cap_rights_init(&rights, CAP_READ, CAP_MMAP_X), 118*d0f245d2SMark Johnston (const char *[]){ "CAP_MMAP_RX", NULL, }); 119*d0f245d2SMark Johnston 120*d0f245d2SMark Johnston /* Aliases map back to the main definition. */ 121*d0f245d2SMark Johnston check_sysdecode_cap_rights(fp, &buf, &sz, 122*d0f245d2SMark Johnston cap_rights_init(&rights, CAP_RECV, CAP_SEND), 123*d0f245d2SMark Johnston (const char *[]){ "CAP_READ", "CAP_WRITE", NULL, }); 124*d0f245d2SMark Johnston 125*d0f245d2SMark Johnston /* This set straddles both indices. */ 126*d0f245d2SMark Johnston check_sysdecode_cap_rights(fp, &buf, &sz, 127*d0f245d2SMark Johnston cap_rights_init(&rights, CAP_READ, CAP_KQUEUE), 128*d0f245d2SMark Johnston (const char *[]){ "CAP_READ", "CAP_KQUEUE", NULL, }); 129*d0f245d2SMark Johnston 130*d0f245d2SMark Johnston /* Create a rights set with an unnamed flag. */ 131*d0f245d2SMark Johnston cap_rights_init(&rights, CAP_SEEK); 132*d0f245d2SMark Johnston cap_rights_clear(&rights, CAP_SEEK_TELL); 133*d0f245d2SMark Johnston check_sysdecode_cap_rights(fp, &buf, &sz, 134*d0f245d2SMark Johnston &rights, 135*d0f245d2SMark Johnston (const char *[]){ "CAP_NONE", "unknown rights", NULL, }); 136*d0f245d2SMark Johnston 137*d0f245d2SMark Johnston cap_rights_init(&rights, CAP_SEEK, CAP_KQUEUE_CHANGE); 138*d0f245d2SMark Johnston cap_rights_clear(&rights, CAP_SEEK_TELL); 139*d0f245d2SMark Johnston check_sysdecode_cap_rights(fp, &buf, &sz, 140*d0f245d2SMark Johnston &rights, 141*d0f245d2SMark Johnston (const char *[]){ "CAP_KQUEUE_CHANGE", "unknown rights", NULL, }); 142*d0f245d2SMark Johnston 143*d0f245d2SMark Johnston ATF_REQUIRE(fclose(fp) == 0); 144*d0f245d2SMark Johnston free(buf); 145*d0f245d2SMark Johnston } 146*d0f245d2SMark Johnston 147*d0f245d2SMark Johnston ATF_TP_ADD_TCS(tp) 148*d0f245d2SMark Johnston { 149*d0f245d2SMark Johnston ATF_TP_ADD_TC(tp, cap_rights); 150*d0f245d2SMark Johnston 151*d0f245d2SMark Johnston return (atf_no_error()); 152*d0f245d2SMark Johnston } 153