xref: /illumos-gate/usr/src/test/os-tests/tests/secflags/secflags_dts.sh (revision 6446bd46ed1b4e9f69da153665f82181ccaedad5)
1#! /usr/bin/ksh
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright 2015, Richard Lowe.
16# Copyright 2019 Joyent, Inc.
17# Copyright 2021 Oxide Computer Company
18#
19
20tmpdir=/tmp/test.$$
21mkdir $tmpdir
22cd $tmpdir
23
24cleanup() {
25    cd /
26    rm -fr $tmpdir
27}
28
29trap 'cleanup' EXIT
30
31cat > tester.c <<EOF
32#include <stdio.h>
33#include <unistd.h>
34
35int
36main(int argc, char **argv)
37{
38	sleep(10000);
39	return (0);
40}
41EOF
42
43gcc -m32 -o tester-aslr.32 tester.c -Wl,-z,aslr=enabled
44gcc -m32 -o tester-noaslr.32 tester.c -Wl,-z,aslr=disabled
45gcc -m64 -o tester-aslr.64 tester.c -Wl,-z,aslr=enabled
46gcc -m64 -o tester-noaslr.64 tester.c -Wl,-z,aslr=disabled
47
48# This is the easiest way I've found to get many many DTs, but it's gross
49gcc -m32 -o many-dts-aslr.32 tester.c -Wl,-z,aslr=enabled \
50    $(for elt in /usr/lib/lib*.so; do echo -Wl,-N,$(basename $elt); done)
51gcc -m32 -o many-dts-noaslr.32 tester.c -Wl,-z,aslr=disabled \
52    $(for elt in /usr/lib/lib*.so; do echo -Wl,-N,$(basename $elt); done)
53gcc -m64 -o many-dts-aslr.64 tester.c -Wl,-z,aslr=enabled \
54    $(for elt in /usr/lib/64/lib*.so; do echo -Wl,-N,$(basename $elt); done)
55gcc -m64 -o many-dts-noaslr.64 tester.c -Wl,-z,aslr=disabled \
56    $(for elt in /usr/lib/64/lib*.so; do echo -Wl,-N,$(basename $elt); done)
57
58check() {
59    bin=$1
60    state=$2
61    set=$3
62    ret=0
63
64    $bin &
65    pid=$!
66    sleep 1
67    psecflags $pid | grep -q "${set}:.*aslr"
68    (( $? != state )) && ret=1
69    kill -9 $pid
70    return $ret
71}
72
73fail() {
74    echo $@
75    exit 1
76}
77
78psecflags -s none $$
79check ./tester-aslr.32 0 E || fail "DT_SUNW_ASLR 1 failed (32-bit)"
80check ./many-dts-aslr.32 0 E || fail \
81    "DT_SUNW_ASLR 1 with many DTs failed (32-bit)"
82check ./tester-aslr.32 1 I || fail \
83    "DT_SUNW_ASLR 1 incorrectly set the inheritable flag (32-bit)"
84check ./tester-aslr.64 0 E || fail "DT_SUNW_ASLR 1 failed (64-bit)"
85check ./many-dts-aslr.64 0 E || fail \
86    "DT_SUNW_ASLR 1 with many DTs failed (64-bit)"
87check ./tester-aslr.64 1 I || fail \
88    "DT_SUNW_ASLR 1 incorrectly set the inheritable flag (64-bit)"
89
90psecflags -s aslr $$
91check ./tester-noaslr.32 1 E || fail "DT_SUNW_ASLR 0 failed (32-bit)"
92check ./many-dts-noaslr.32 1 E || fail  \
93    "DT_SUNW_ASLR 0 with many DTs failed (32-bit)"
94check ./tester-noaslr.64 1 E || fail "DT_SUNW_ASLR 0 failed (64-bit)"
95check ./many-dts-noaslr.64 1 E || fail \
96    "DT_SUNW_ASLR 0 with many DTs failed (64-bit)"
97