xref: /freebsd/lib/libveriexec/gbl_check.c (revision 1554ba03)
11554ba03SSimon J. Gerraty /*
21554ba03SSimon J. Gerraty  * SPDX-License-Identifier: BSD-2-Clause
31554ba03SSimon J. Gerraty  *
41554ba03SSimon J. Gerraty  * Copyright (c) 2019-2023, Juniper Networks, Inc.
51554ba03SSimon J. Gerraty  * All rights reserved.
61554ba03SSimon J. Gerraty  *
71554ba03SSimon J. Gerraty  * Redistribution and use in source and binary forms, with or without
81554ba03SSimon J. Gerraty  * modification, are permitted provided that the following conditions
91554ba03SSimon J. Gerraty  * are met:
101554ba03SSimon J. Gerraty  * 1. Redistributions of source code must retain the above copyright
111554ba03SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer.
121554ba03SSimon J. Gerraty  * 2. Redistributions in binary form must reproduce the above copyright
131554ba03SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer in the
141554ba03SSimon J. Gerraty  *    documentation and/or other materials provided with the distribution.
151554ba03SSimon J. Gerraty  *
161554ba03SSimon J. Gerraty  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171554ba03SSimon J. Gerraty  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181554ba03SSimon J. Gerraty  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191554ba03SSimon J. Gerraty  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201554ba03SSimon J. Gerraty  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211554ba03SSimon J. Gerraty  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221554ba03SSimon J. Gerraty  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231554ba03SSimon J. Gerraty  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241554ba03SSimon J. Gerraty  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251554ba03SSimon J. Gerraty  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261554ba03SSimon J. Gerraty  * SUCH DAMAGE.
271554ba03SSimon J. Gerraty  *
281554ba03SSimon J. Gerraty  */
291554ba03SSimon J. Gerraty 
301554ba03SSimon J. Gerraty #include <sys/types.h>
311554ba03SSimon J. Gerraty #include <sys/errno.h>
321554ba03SSimon J. Gerraty #include <sys/mac.h>
331554ba03SSimon J. Gerraty 
341554ba03SSimon J. Gerraty #include <unistd.h>
351554ba03SSimon J. Gerraty #include <fcntl.h>
361554ba03SSimon J. Gerraty 
371554ba03SSimon J. Gerraty #include <security/mac_grantbylabel/mac_grantbylabel.h>
381554ba03SSimon J. Gerraty 
391554ba03SSimon J. Gerraty /**
401554ba03SSimon J. Gerraty  * @brief does path have a gbl label
411554ba03SSimon J. Gerraty  *
421554ba03SSimon J. Gerraty  * @return
431554ba03SSimon J. Gerraty  * @li 0 if no/empty label or module not loaded
441554ba03SSimon J. Gerraty  * @li value of label
451554ba03SSimon J. Gerraty  */
461554ba03SSimon J. Gerraty unsigned int
gbl_check_path(const char * path)471554ba03SSimon J. Gerraty gbl_check_path(const char *path)
481554ba03SSimon J. Gerraty {
491554ba03SSimon J. Gerraty 	struct mac_grantbylabel_fetch_gbl_args gbl;
501554ba03SSimon J. Gerraty 	int fd;
511554ba03SSimon J. Gerraty 	int rc;
521554ba03SSimon J. Gerraty 
531554ba03SSimon J. Gerraty 	rc = 0;
541554ba03SSimon J. Gerraty 	if ((fd = open(path, O_RDONLY|O_VERIFY)) >= 0) {
551554ba03SSimon J. Gerraty 		gbl.u.fd = fd;
561554ba03SSimon J. Gerraty 		if (mac_syscall(MAC_GRANTBYLABEL_NAME,
571554ba03SSimon J. Gerraty 			MAC_GRANTBYLABEL_FETCH_GBL,
581554ba03SSimon J. Gerraty 			&gbl) == 0) {
591554ba03SSimon J. Gerraty 			if (gbl.gbl != GBL_EMPTY)
601554ba03SSimon J. Gerraty 				rc = gbl.gbl;
611554ba03SSimon J. Gerraty 		}
621554ba03SSimon J. Gerraty 		close(fd);
631554ba03SSimon J. Gerraty 	}
641554ba03SSimon J. Gerraty 	return(rc);
651554ba03SSimon J. Gerraty }
661554ba03SSimon J. Gerraty 
671554ba03SSimon J. Gerraty /**
681554ba03SSimon J. Gerraty  * @brief does pid have a gbl label
691554ba03SSimon J. Gerraty  *
701554ba03SSimon J. Gerraty  * @return
711554ba03SSimon J. Gerraty  * @li 0 if no/empty label or module not loaded
721554ba03SSimon J. Gerraty  * @li value of label
731554ba03SSimon J. Gerraty  */
741554ba03SSimon J. Gerraty unsigned int
gbl_check_pid(pid_t pid)751554ba03SSimon J. Gerraty gbl_check_pid(pid_t pid)
761554ba03SSimon J. Gerraty {
771554ba03SSimon J. Gerraty 	struct mac_grantbylabel_fetch_gbl_args gbl;
781554ba03SSimon J. Gerraty 	int rc;
791554ba03SSimon J. Gerraty 
801554ba03SSimon J. Gerraty 	rc = 0;
811554ba03SSimon J. Gerraty 	gbl.u.pid = pid;
821554ba03SSimon J. Gerraty 	if (mac_syscall(MAC_GRANTBYLABEL_NAME,
831554ba03SSimon J. Gerraty 		MAC_GRANTBYLABEL_FETCH_PID_GBL, &gbl) == 0) {
841554ba03SSimon J. Gerraty 		if (gbl.gbl != GBL_EMPTY)
851554ba03SSimon J. Gerraty 			rc = gbl.gbl;
861554ba03SSimon J. Gerraty 	}
871554ba03SSimon J. Gerraty 	return(rc);
881554ba03SSimon J. Gerraty }
891554ba03SSimon J. Gerraty 
901554ba03SSimon J. Gerraty 
911554ba03SSimon J. Gerraty #ifdef UNIT_TEST
921554ba03SSimon J. Gerraty #include <stdlib.h>
931554ba03SSimon J. Gerraty #include <stdio.h>
941554ba03SSimon J. Gerraty #include <err.h>
951554ba03SSimon J. Gerraty 
961554ba03SSimon J. Gerraty int
main(int argc,char * argv[])971554ba03SSimon J. Gerraty main(int argc, char *argv[])
981554ba03SSimon J. Gerraty {
991554ba03SSimon J. Gerraty 	pid_t pid;
1001554ba03SSimon J. Gerraty 	int pflag = 0;
1011554ba03SSimon J. Gerraty 	int c;
1021554ba03SSimon J. Gerraty 	unsigned int gbl;
1031554ba03SSimon J. Gerraty 
1041554ba03SSimon J. Gerraty 	while ((c = getopt(argc, argv, "p")) != -1) {
1051554ba03SSimon J. Gerraty 		switch (c) {
1061554ba03SSimon J. Gerraty 		case 'p':
1071554ba03SSimon J. Gerraty 			pflag = 1;
1081554ba03SSimon J. Gerraty 			break;
1091554ba03SSimon J. Gerraty 		default:
1101554ba03SSimon J. Gerraty 			break;
1111554ba03SSimon J. Gerraty 		}
1121554ba03SSimon J. Gerraty 	}
1131554ba03SSimon J. Gerraty 	for (; optind < argc; optind++) {
1141554ba03SSimon J. Gerraty 
1151554ba03SSimon J. Gerraty 		if (pflag) {
1161554ba03SSimon J. Gerraty 			pid = atoi(argv[optind]);
1171554ba03SSimon J. Gerraty 			gbl = gbl_check_pid(pid);
1181554ba03SSimon J. Gerraty 		} else {
1191554ba03SSimon J. Gerraty 			gbl = gbl_check_path(argv[optind]);
1201554ba03SSimon J. Gerraty 		}
1211554ba03SSimon J. Gerraty 		printf("arg=%s, gbl=%#o\n", argv[optind], gbl);
1221554ba03SSimon J. Gerraty 	}
1231554ba03SSimon J. Gerraty 	return 0;
1241554ba03SSimon J. Gerraty }
1251554ba03SSimon J. Gerraty #endif
126