Path: blob/main/tools/test/stress2/misc/beneath4.sh
39536 views
#!/bin/sh12#3# SPDX-License-Identifier: BSD-2-Clause4#5# Copyright (c) 2021 Peter Holm <[email protected]>6#7# Redistribution and use in source and binary forms, with or without8# modification, are permitted provided that the following conditions9# are met:10# 1. Redistributions of source code must retain the above copyright11# notice, this list of conditions and the following disclaimer.12# 2. Redistributions in binary form must reproduce the above copyright13# notice, this list of conditions and the following disclaimer in the14# documentation and/or other materials provided with the distribution.15#16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26# SUCH DAMAGE.27#2829# Test of:30# AT_BENEATH 0x1000 /* Fail if not under dirfd */31# AT_RESOLVE_BENEATH 0x2000 /* As AT_BENEATH, but do not allow32# resolve to walk out of dirfd even3334dir=/tmp/beneath4.dir35rm -rf $dir36mkdir -p $dir37here=`pwd`38cd $dir3940cat > beneath4.c <<EOF41#include <sys/stat.h>4243#include <err.h>44#include <errno.h>45#include <fcntl.h>46#include <stdio.h>47#include <stdlib.h>48#include <string.h>49#include <unistd.h>5051int52main(int argc, char *argv[])53{54struct stat st;55int exp, fd, flag, r;56char *cwd, *dir, *obj, *s;5758if (argc != 5) {59fprintf(stderr,60"Usage: %s <dir> <test obj> <flag> <expected return>\n",61argv[0]);62return (1);63}6465cwd = getwd(NULL);66dir = argv[1];67obj = argv[2];68sscanf(argv[3], "%x", &flag);69exp = atoi(argv[4]);70#if 071if ((flag & AT_RESOLVE_BENEATH) == 0) {72fprintf(stderr, "Flag must be %#x or %#x\n",73AT_BENEATH, AT_RESOLVE_BENEATH);74return (1);75}76#endif77if ((fd = open(dir, O_DIRECTORY | O_RDONLY)) == -1)78err(1, "open(%s)", dir);7980if (fstatat(fd, obj, &st, flag) == -1)81r = errno;82else83r = 0;84s = "FAIL";85if (r == exp)86s = "OK";87warn("cwd=%s, top=%s. flag=%0.6x. fstatf(%s) = %2d (expect %2d). %4s",88cwd, dir, flag, obj, r, exp, s);8990return (r != exp);91}92EOF93cc -o beneath4 -Wall -Wextra -O2 -g beneath4.c || exit 194rm beneath4.c9596mkdir -p /tmp/beneath4.dir/a/a97touch /tmp/beneath4.dir/a/f98ln /tmp/beneath4.dir/a/f /tmp/beneath4.dir/a/c99ln -s /tmp/beneath4.dir/a/a /tmp/beneath4.dir/a/d100ln -s /tmp/beneath4.dir/a/b /tmp/beneath4.dir/a/e101mkfifo /tmp/beneath4.dir/a/fifo102103top=$dir/a104105cd $here106s=0107#ls -lR $dir108#echo AT_BENEATH109#$dir/beneath4 $top a 0x1000 0 || s=1110#$dir/beneath4 $top b 0x1000 2 || s=1111#$dir/beneath4 $top c 0x1000 0 || s=1112#$dir/beneath4 $top d 0x1000 0 || s=1113#$dir/beneath4 $top e 0x1000 2 || s=1114#$dir/beneath4 $top fifo 0x1000 0 || s=1115#$dir/beneath4 $top $top/../../beneath4.d/a/a 0x1000 93 || s=1116#$dir/beneath4 $top $top/.. 0x1000 93 || s=1117#$dir/beneath4 $top ../a 0x1000 0 || s=1118119printf "\nAT_RESOLVE_BENEATH\n"120$dir/beneath4 $top a 0x2000 0 || s=1121$dir/beneath4 $top b 0x2000 2 || s=1122$dir/beneath4 $top c 0x2000 0 || s=1123$dir/beneath4 $top d 0x2000 93 || s=1124$dir/beneath4 $top e 0x2000 93 || s=1125$dir/beneath4 $top fifo 0x2000 0 || s=1126$dir/beneath4 $top $top/../../beneath4.d/a/a 0x2000 93 || s=1127$dir/beneath4 $top $top/.. 0x2000 93 || s=1128$dir/beneath4 $top ../a 0x2000 93 || s=1129printf "\nNo flag\n"130$dir/beneath4 $top ../a 0x0000 0 || s=1131rm -rf $dir132exit $s133134135