(root)/
strace-6.5/
tests-mx32/
overflowuid.c
       1  /*
       2   * Copyright (c) 2014-2021 Dmitry V. Levin <ldv@strace.io>
       3   * All rights reserved.
       4   *
       5   * SPDX-License-Identifier: GPL-2.0-or-later
       6   */
       7  
       8  #include "tests.h"
       9  #include <errno.h>
      10  #include <fcntl.h>
      11  #include <limits.h>
      12  #include <stdlib.h>
      13  #include <unistd.h>
      14  
      15  int
      16  read_int_from_file(const char *const fname, int *const pvalue)
      17  {
      18  	const int fd = open(fname, O_RDONLY);
      19  	if (fd < 0)
      20  		return -1;
      21  
      22  	long lval;
      23  	char buf[sizeof(lval) * 3];
      24  	int n = read(fd, buf, sizeof(buf) - 1);
      25  	int saved_errno = errno;
      26  	close(fd);
      27  
      28  	if (n < 0) {
      29  		errno = saved_errno;
      30  		return -1;
      31  	}
      32  
      33  	buf[n] = '\0';
      34  	char *endptr = 0;
      35  	errno = 0;
      36  	lval = strtol(buf, &endptr, 10);
      37  	if (!endptr || (*endptr && '\n' != *endptr)
      38  #if INT_MAX < LONG_MAX
      39  	    || lval > INT_MAX || lval < INT_MIN
      40  #endif
      41  	    || ERANGE == errno) {
      42  		if (!errno)
      43  			errno = EINVAL;
      44  		return -1;
      45  	}
      46  
      47  	*pvalue = (int) lval;
      48  	return 0;
      49  }
      50  
      51  static void
      52  check_overflow_id(const int id, const char *overflowid)
      53  {
      54  	int n;
      55  
      56  	if (read_int_from_file(overflowid, &n)) {
      57  		if (ENOENT == errno)
      58  			return;
      59  		perror_msg_and_fail("read_int_from_file: %s", overflowid);
      60  	}
      61  
      62  	if (id == n)
      63  		error_msg_and_skip("%d matches %s", id, overflowid);
      64  }
      65  
      66  void
      67  check_overflowuid(const int uid)
      68  {
      69  	check_overflow_id(uid, "/proc/sys/kernel/overflowuid");
      70  }
      71  
      72  void
      73  check_overflowgid(const int gid)
      74  {
      75  	check_overflow_id(gid, "/proc/sys/kernel/overflowgid");
      76  }