Print this page
11838 secflag tests are racy
   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 # Copyright 2015, Richard Lowe.

  15 
  16 tmpdir=/tmp/test.$$
  17 mkdir $tmpdir
  18 cd $tmpdir
  19 
  20 cleanup() {
  21     cd /
  22     rm -fr $tmpdir
  23 }
  24 
  25 trap 'cleanup' EXIT
  26 
  27 cat > tester.c <<EOF
  28 #include <stdio.h>
  29 #include <unistd.h>
  30 
  31 int
  32 main(int argc, char **argv)
  33 {
  34         sleep(10000);
  35         return (0);
  36 }
  37 EOF
  38 
  39 gcc -o tester-aslr tester.c -Wl,-z,aslr=enabled
  40 gcc -o tester-noaslr tester.c -Wl,-z,aslr=disabled
  41 
  42 # This is the easiest way I've found to get many many DTs, but it's gross
  43 gcc -o many-dts-aslr tester.c -Wl,-z,aslr=enabled $(for elt in /usr/lib/lib*.so; do echo -Wl,-N,$(basename $elt); done)
  44 gcc -o many-dts-noaslr tester.c -Wl,-z,aslr=disabled $(for elt in /usr/lib/lib*.so; do echo -Wl,-N,$(basename $elt); done)
  45 
  46 check() {
  47     bin=$1
  48     state=$2
  49     set=$3
  50     ret=0
  51 
  52     $bin &
  53     pid=$!

  54     psecflags $pid | grep -q "${set}:.*aslr"
  55     (( $? != $state )) && ret=1
  56     kill -9 $pid
  57     return $ret
  58 }
  59 
  60 fail() {
  61     echo $@
  62     exit 1
  63 }
  64 
  65 psecflags -s none $$
  66 check ./tester-aslr 0 E || fail "DT_SUNW_ASLR 1 failed"
  67 check ./many-dts-aslr 0 E || fail "DT_SUNW_ASLR 1 with many DTs failed"
  68 check ./tester-aslr 1 I || fail "DT_SUNW_ASLR 1 incorrectly set the inheritable flag"
  69 
  70 psecflags -s aslr $$
  71 check ./tester-noaslr 1 E || fail "DT_SUNW_ASLR 0 failed"
  72 check ./many-dts-noaslr 1 E || fail "DT_SUNW_ASLR 0 with many DTs failed"
  73 
   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 # Copyright 2015, Richard Lowe.
  15 # Copyright 2019 Joyent, Inc.
  16 
  17 tmpdir=/tmp/test.$$
  18 mkdir $tmpdir
  19 cd $tmpdir
  20 
  21 cleanup() {
  22     cd /
  23     rm -fr $tmpdir
  24 }
  25 
  26 trap 'cleanup' EXIT
  27 
  28 cat > tester.c <<EOF
  29 #include <stdio.h>
  30 #include <unistd.h>
  31 
  32 int
  33 main(int argc, char **argv)
  34 {
  35         sleep(10000);
  36         return (0);
  37 }
  38 EOF
  39 
  40 gcc -o tester-aslr tester.c -Wl,-z,aslr=enabled
  41 gcc -o tester-noaslr tester.c -Wl,-z,aslr=disabled
  42 
  43 # This is the easiest way I've found to get many many DTs, but it's gross
  44 gcc -o many-dts-aslr tester.c -Wl,-z,aslr=enabled $(for elt in /usr/lib/lib*.so; do echo -Wl,-N,$(basename $elt); done)
  45 gcc -o many-dts-noaslr tester.c -Wl,-z,aslr=disabled $(for elt in /usr/lib/lib*.so; do echo -Wl,-N,$(basename $elt); done)
  46 
  47 check() {
  48     bin=$1
  49     state=$2
  50     set=$3
  51     ret=0
  52 
  53     $bin &
  54     pid=$!
  55     sleep 1
  56     psecflags $pid | grep -q "${set}:.*aslr"
  57     (( $? != $state )) && ret=1
  58     kill -9 $pid
  59     return $ret
  60 }
  61 
  62 fail() {
  63     echo $@
  64     exit 1
  65 }
  66 
  67 psecflags -s none $$
  68 check ./tester-aslr 0 E || fail "DT_SUNW_ASLR 1 failed"
  69 check ./many-dts-aslr 0 E || fail "DT_SUNW_ASLR 1 with many DTs failed"
  70 check ./tester-aslr 1 I || fail "DT_SUNW_ASLR 1 incorrectly set the inheritable flag"
  71 
  72 psecflags -s aslr $$
  73 check ./tester-noaslr 1 E || fail "DT_SUNW_ASLR 0 failed"
  74 check ./many-dts-noaslr 1 E || fail "DT_SUNW_ASLR 0 with many DTs failed"
  75