about summary refs log tree commit diff
path: root/elf/tst-env-setuid-tunables.c
blob: a47219047f0f602d864d27b47d9a1e4d59f1e85e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Copyright (C) 2017-2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* Verify that GLIBC_TUNABLES is kept unchanged but no tunable is actually
   enabled for AT_SECURE processes.  */

#include <dl-tunables.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <intprops.h>
#include <array_length.h>

#include <support/check.h>
#include <support/support.h>
#include <support/test-driver.h>
#include <support/capture_subprocess.h>

static const char *teststrings[] =
{
  "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
  "glibc.malloc.check=2:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
  "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
  "glibc.malloc.perturb=0x800",
  "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
  "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
  "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
  "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
  "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
  "glibc.malloc.check=2",
  "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
  "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
  ":glibc.malloc.garbage=2:glibc.malloc.check=1",
  "glibc.malloc.check=1:glibc.malloc.check=2",
  "not_valid.malloc.check=2",
  "glibc.not_valid.check=2",
};

static int
test_child (int off)
{
  const char *val = getenv ("GLIBC_TUNABLES");
  int ret = 1;

  printf ("    [%d] GLIBC_TUNABLES is %s\n", off, val);
  fflush (stdout);
  if (val != NULL)
    printf ("    [%d] Unexpected GLIBC_TUNABLES VALUE %s\n", off, val);
  else
    ret = 0;
  fflush (stdout);

  /* Also check if the set tunables are effectively unchanged.  */
  int32_t check = TUNABLE_GET_FULL (glibc, malloc, check, int32_t, NULL);
  size_t mmap_threshold = TUNABLE_GET_FULL (glibc, malloc, mmap_threshold,
					    size_t, NULL);
  int32_t perturb = TUNABLE_GET_FULL (glibc, malloc, perturb, int32_t, NULL);

  printf ("    [%d] glibc.malloc.check=%d\n", off, check);
  fflush (stdout);
  printf ("    [%d] glibc.malloc.mmap_threshold=%zu\n", off, mmap_threshold);
  fflush (stdout);
  printf ("    [%d] glibc.malloc.perturb=%d\n", off, perturb);
  fflush (stdout);

  ret |= check != 0;
  ret |= mmap_threshold != 0;
  ret |= perturb != 0;

  return ret;
}

static int
do_test (int argc, char **argv)
{
  /* Setgid child process.  */
  if (argc == 2)
    {
      if (getgid () == getegid ())
	/* This can happen if the file system is mounted nosuid.  */
	FAIL_UNSUPPORTED ("SGID failed: GID and EGID match (%jd)\n",
			  (intmax_t) getgid ());

      int ret = test_child (atoi (argv[1]));

      if (ret != 0)
	exit (1);

      /* Special return code to make sure that the child executed all the way
	 through.  */
      exit (42);
    }
  else
    {
      /* Spawn tests.  */
      for (int i = 0; i < array_length (teststrings); i++)
	{
	  char buf[INT_BUFSIZE_BOUND (int)];

	  printf ("[%d] Spawned test for %s\n", i, teststrings[i]);
	  snprintf (buf, sizeof (buf), "%d\n", i);
	  fflush (stdout);
	  if (setenv ("GLIBC_TUNABLES", teststrings[i], 1) != 0)
	    {
	      printf ("    [%d] Failed to set GLIBC_TUNABLES: %m", i);
	      support_record_failure ();
	      continue;
	    }

	  int status = support_capture_subprogram_self_sgid (buf);

	  /* Bail out early if unsupported.  */
	  if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
	    return EXIT_UNSUPPORTED;

	  if (WEXITSTATUS (status) != 42)
	    {
	      printf ("    [%d] child failed with status %d\n", i,
		      WEXITSTATUS (status));
	      support_record_failure ();
	    }
	}
      return 0;
    }
}

#define TEST_FUNCTION_ARGV do_test
#include <support/test-driver.c>