xref: /freebsd/tools/test/stress2/misc/setuid.sh (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# Regression test for r218019: "panic: oof, we didn't get our fd"
30
31# Page fault seen:
32# https://people.freebsd.org/~pho/stress/log/kostik895.txt
33# Caused by r300792 + r300793.
34# Fixed by r301580.
35
36[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
37
38. ../default.cfg
39
40odir=`pwd`
41cd /tmp
42sed '1,/^EOF/d' < $odir/$0 > setuid.c
43rm -f /tmp/setuid
44mycc -o setuid -Wall -Wextra -O2 -g setuid.c -static || exit 1
45rm -f setuid.c
46
47mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
48mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
49
50mdconfig -a -t swap -s 2g -u $mdstart || exit 1
51newfs $newfs_flags md$mdstart > /dev/null
52mount /dev/md$mdstart $mntpoint
53chmod 777 $mntpoint
54mkdir $mntpoint/root
55
56cp /tmp/setuid $mntpoint/root/nop
57chown nobody:nobody $mntpoint/root/nop
58chmod 4755 $mntpoint/root/nop
59
60chmod 777 $mntpoint/root
61
62echo "Expect Abort trap"
63./setuid $mntpoint/root 1
64./setuid $mntpoint/root 2
65./setuid $mntpoint/root 3
66./setuid $mntpoint/root 4
67./setuid $mntpoint/root 5
68./setuid $mntpoint/root 6
69./setuid $mntpoint/root 7
70
71while mount | grep "on $mntpoint " | grep -q /dev/md; do
72	umount $mntpoint || sleep 1
73done
74mdconfig -d -u $mdstart
75rm -f /tmp/setuid /tmp/nop
76exit
77EOF
78#include <sys/types.h>
79#include <pwd.h>
80#include <err.h>
81#include <stdio.h>
82#include <stdlib.h>
83#include <unistd.h>
84
85int
86main(int argc, char **argv)
87{
88	char *av[2];
89	int fd;
90
91	if (argc == 1)
92		return (0);
93
94	if (chroot(argv[1]) != 0)
95		err(1, "chroot(%s)", argv[1]);
96	fd = atoi(argv[2]);
97
98	if (fd & 1) {
99		fprintf(stderr, "Close fd 0 ");
100		close(0);
101	}
102	if (fd & 2) {
103		fprintf(stderr, "Close fd 1 ");
104		close(1);
105	}
106	if (fd & 4) {
107		fprintf(stderr, "Close fd 2\n");
108		close(2);
109	} else
110		fprintf(stderr, "\n");
111
112	if (chdir("/") != 0)
113		err(1, "chdir");
114	av[0] = "/nop";
115	av[1] = 0;
116	if (execve(av[0], av, NULL) == -1)
117		err(1, "execve");
118
119	return (0);
120}
121