Blame support/support_ptrace.c

Packit Service b43375
/* Support functions handling ptrace_scope.
Packit Service b43375
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Service b43375
   This file is part of the GNU C Library.
Packit Service b43375
Packit Service b43375
   The GNU C Library is free software; you can redistribute it and/or
Packit Service b43375
   modify it under the terms of the GNU Lesser General Public
Packit Service b43375
   License as published by the Free Software Foundation; either
Packit Service b43375
   version 2.1 of the License, or (at your option) any later version.
Packit Service b43375
Packit Service b43375
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service b43375
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service b43375
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service b43375
   Lesser General Public License for more details.
Packit Service b43375
Packit Service b43375
   You should have received a copy of the GNU Lesser General Public
Packit Service b43375
   License along with the GNU C Library; if not, see
Packit Service b43375
   <https://www.gnu.org/licenses/>.  */
Packit Service b43375
Packit Service b43375
#include <support/check.h>
Packit Service b43375
#include <support/xstdio.h>
Packit Service b43375
#include <support/xptrace.h>
Packit Service b43375
#include <sys/prctl.h>
Packit Service b43375
Packit Service b43375
int
Packit Service b43375
support_ptrace_scope (void)
Packit Service b43375
{
Packit Service b43375
  int ptrace_scope = -1;
Packit Service b43375
Packit Service b43375
#ifdef __linux__
Packit Service b43375
  /* YAMA may be not enabled.  Otherwise it contains a value from 0 to 3:
Packit Service b43375
     - 0 classic ptrace permissions
Packit Service b43375
     - 1 restricted ptrace
Packit Service b43375
     - 2 admin-only attach
Packit Service b43375
     - 3 no attach  */
Packit Service b43375
  FILE *f = fopen ("/proc/sys/kernel/yama/ptrace_scope", "r");
Packit Service b43375
  if (f != NULL)
Packit Service b43375
    {
Packit Service b43375
      TEST_COMPARE (fscanf (f, "%d", &ptrace_scope), 1);
Packit Service b43375
      xfclose (f);
Packit Service b43375
    }
Packit Service b43375
#endif
Packit Service b43375
Packit Service b43375
  return ptrace_scope;
Packit Service b43375
}