xref: /freebsd/tools/test/stress2/misc/setuid.sh (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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
51bsdlabel -w md$mdstart auto
52newfs $newfs_flags md${mdstart}$part > /dev/null
53mount /dev/md${mdstart}$part $mntpoint
54chmod 777 $mntpoint
55mkdir $mntpoint/root
56
57cp /tmp/setuid $mntpoint/root/nop
58chown nobody:nobody $mntpoint/root/nop
59chmod 4755 $mntpoint/root/nop
60
61chmod 777 $mntpoint/root
62
63echo "Expect Abort trap"
64./setuid $mntpoint/root 1
65./setuid $mntpoint/root 2
66./setuid $mntpoint/root 3
67./setuid $mntpoint/root 4
68./setuid $mntpoint/root 5
69./setuid $mntpoint/root 6
70./setuid $mntpoint/root 7
71
72while mount | grep "on $mntpoint " | grep -q /dev/md; do
73	umount $mntpoint || sleep 1
74done
75mdconfig -d -u $mdstart
76rm -f /tmp/setuid /tmp/nop
77exit
78EOF
79#include <sys/types.h>
80#include <pwd.h>
81#include <err.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <unistd.h>
85
86int
87main(int argc, char **argv)
88{
89	char *av[2];
90	int fd;
91
92	if (argc == 1)
93		return (0);
94
95	if (chroot(argv[1]) != 0)
96		err(1, "chroot(%s)", argv[1]);
97	fd = atoi(argv[2]);
98
99	if (fd & 1) {
100		fprintf(stderr, "Close fd 0 ");
101		close(0);
102	}
103	if (fd & 2) {
104		fprintf(stderr, "Close fd 1 ");
105		close(1);
106	}
107	if (fd & 4) {
108		fprintf(stderr, "Close fd 2\n");
109		close(2);
110	} else
111		fprintf(stderr, "\n");
112
113	if (chdir("/") != 0)
114		err(1, "chdir");
115	av[0] = "/nop";
116	av[1] = 0;
117	if (execve(av[0], av, NULL) == -1)
118		err(1, "execve");
119
120	return (0);
121}
122