1
0
mirror of https://github.com/openbsd/src.git synced 2026-04-23 05:34:18 +00:00

Add __pledge_open regress.

This commit is contained in:
dgl
2026-03-27 05:06:33 +00:00
parent 019066103c
commit 69556c7634
2 changed files with 111 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
# $OpenBSD: Makefile,v 1.1 2026/03/27 05:06:33 dgl Exp $
PROG= open
LDFLAGS= -static
run-no-pledge: ${PROG}
./${PROG} "[NO PLEDGE]" /etc/myname
REGRESS_TARGETS+= run-no-pledge
run-unknown-file: ${PROG}
if ./${PROG} "stdio" /etc/myname; \
then false; else [[ $$? == 134 ]]; fi
REGRESS_TARGETS+= run-unknown-file
run-hosts: ${PROG}
./${PROG} "stdio dns fattr" /etc/hosts
REGRESS_TARGETS+= run-hosts
run-hosts-no-dns: ${PROG}
if ./${PROG} "stdio" /etc/hosts; \
then false; else [[ $$? == 134 ]]; fi
REGRESS_TARGETS+= run-hosts-no-dns
run-hosts-no-dns-error: ${PROG}
if ./${PROG} "stdio error" /etc/hosts; \
then false; else [[ $$? == 2 ]]; fi
REGRESS_TARGETS+= run-hosts-no-dns-error
run-hosts-no-dns-but-rpath: ${PROG}
./${PROG} "stdio rpath fattr" /etc/hosts
REGRESS_TARGETS+= run-hosts-no-dns-but-rpath
run-devnull: ${PROG}
./${PROG} "stdio" /dev/null
REGRESS_TARGETS+= run-devnull
run-empty-promises: ${PROG}
if ./${PROG} "" /dev/null; \
then false; else [[ $$? == 134 ]]; fi
REGRESS_TARGETS+= run-empty-promises
run-zoneinfo: ${PROG}
./${PROG} "stdio fattr" /usr/share/zoneinfo/Asia/Ulaanbaatar
REGRESS_TARGETS+= run-zoneinfo
run-zoneinfo-dotdot: ${PROG}
if ./${PROG} "stdio" /usr/share/zoneinfo/Asia/..; \
then false; else [[ $$? == 134 ]]; fi
REGRESS_TARGETS+= run-zoneinfo-dotdot
.include <bsd.regress.mk>

View File

@@ -0,0 +1,60 @@
/* $OpenBSD: open.c,v 1.1 2026/03/27 05:06:33 dgl Exp $ */
/*
* Copyright (c) 2026 David Leadbeater <dgl@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <err.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
/*
* __pledge_open(2) is libc internal. This is for testing kernel semantics
* only, do not use this outside of regress.
*/
int _libc___pledge_open(char *path, int flags, ...);
#define NO_PLEDGE "[NO PLEDGE]"
int
main(int argc, char **argv)
{
int fd;
char *promise, *path;
struct stat sb;
if (argc != 3)
errx(1, "argc: %d", argc);
promise = argv[1];
path = argv[2];
if (strcmp(promise, NO_PLEDGE) != 0 && pledge(promise, NULL) == -1)
err(1, "pledge %s", promise);
fd = _libc___pledge_open(path, O_RDONLY);
if (fd == -1)
err(2, "open %s", path);
if (fstat(fd, &sb) == -1)
err(3, "fstat %s", path);
/* __pledge_open marks fds so certain operations are not allowed. */
if (strcmp(promise, NO_PLEDGE) != 0 && S_ISREG(sb.st_mode))
if (fchmod(fd, 0) != -1)
errx(4, "fchmod succeeded");
}