1*ba1276acSMatthew Dillon /*
2*ba1276acSMatthew Dillon * Copyright (c) 2016 Darren Tucker. All rights reserved.
3*ba1276acSMatthew Dillon *
4*ba1276acSMatthew Dillon * Permission to use, copy, modify, and distribute this software for any
5*ba1276acSMatthew Dillon * purpose with or without fee is hereby granted, provided that the above
6*ba1276acSMatthew Dillon * copyright notice and this permission notice appear in all copies.
7*ba1276acSMatthew Dillon *
8*ba1276acSMatthew Dillon * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*ba1276acSMatthew Dillon * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*ba1276acSMatthew Dillon * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*ba1276acSMatthew Dillon * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*ba1276acSMatthew Dillon * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13*ba1276acSMatthew Dillon * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14*ba1276acSMatthew Dillon * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*ba1276acSMatthew Dillon */
16*ba1276acSMatthew Dillon
17*ba1276acSMatthew Dillon #include "includes.h"
18*ba1276acSMatthew Dillon
19*ba1276acSMatthew Dillon #include <sys/types.h>
20*ba1276acSMatthew Dillon #ifdef HAVE_SYS_PROCCTL_H
21*ba1276acSMatthew Dillon #include <sys/procctl.h>
22*ba1276acSMatthew Dillon #endif
23*ba1276acSMatthew Dillon #if defined(HAVE_SYS_PRCTL_H)
24*ba1276acSMatthew Dillon #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
25*ba1276acSMatthew Dillon #endif
26*ba1276acSMatthew Dillon #ifdef HAVE_SYS_PTRACE_H
27*ba1276acSMatthew Dillon #include <sys/ptrace.h>
28*ba1276acSMatthew Dillon #endif
29*ba1276acSMatthew Dillon #ifdef HAVE_PRIV_H
30*ba1276acSMatthew Dillon #include <priv.h> /* For setpflags() and __PROC_PROTECT */
31*ba1276acSMatthew Dillon #endif
32*ba1276acSMatthew Dillon #include <stdarg.h>
33*ba1276acSMatthew Dillon #include <stdio.h>
34*ba1276acSMatthew Dillon #include <string.h>
35*ba1276acSMatthew Dillon #include <unistd.h>
36*ba1276acSMatthew Dillon
37*ba1276acSMatthew Dillon #include "log.h"
38*ba1276acSMatthew Dillon
39*ba1276acSMatthew Dillon void
platform_disable_tracing(int strict)40*ba1276acSMatthew Dillon platform_disable_tracing(int strict)
41*ba1276acSMatthew Dillon {
42*ba1276acSMatthew Dillon #if defined(HAVE_PROCCTL) && defined(PROC_TRACE_CTL)
43*ba1276acSMatthew Dillon /* On FreeBSD, we should make this process untraceable */
44*ba1276acSMatthew Dillon int disable_trace = PROC_TRACE_CTL_DISABLE;
45*ba1276acSMatthew Dillon
46*ba1276acSMatthew Dillon /*
47*ba1276acSMatthew Dillon * On FreeBSD, we should make this process untraceable.
48*ba1276acSMatthew Dillon * pid=0 means "this process" but some older kernels do not
49*ba1276acSMatthew Dillon * understand that so retry with our own pid before failing.
50*ba1276acSMatthew Dillon */
51*ba1276acSMatthew Dillon if (procctl(P_PID, 0, PROC_TRACE_CTL, &disable_trace) == 0)
52*ba1276acSMatthew Dillon return;
53*ba1276acSMatthew Dillon if (procctl(P_PID, getpid(), PROC_TRACE_CTL, &disable_trace) == 0)
54*ba1276acSMatthew Dillon return;
55*ba1276acSMatthew Dillon if (strict)
56*ba1276acSMatthew Dillon fatal("unable to make the process untraceable: %s",
57*ba1276acSMatthew Dillon strerror(errno));
58*ba1276acSMatthew Dillon #endif
59*ba1276acSMatthew Dillon #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
60*ba1276acSMatthew Dillon /* Disable ptrace on Linux without sgid bit */
61*ba1276acSMatthew Dillon if (prctl(PR_SET_DUMPABLE, 0) != 0 && strict)
62*ba1276acSMatthew Dillon fatal("unable to make the process undumpable: %s",
63*ba1276acSMatthew Dillon strerror(errno));
64*ba1276acSMatthew Dillon #endif
65*ba1276acSMatthew Dillon #if defined(HAVE_SETPFLAGS) && defined(__PROC_PROTECT)
66*ba1276acSMatthew Dillon /* On Solaris, we should make this process untraceable */
67*ba1276acSMatthew Dillon if (setpflags(__PROC_PROTECT, 1) != 0 && strict)
68*ba1276acSMatthew Dillon fatal("unable to make the process untraceable: %s",
69*ba1276acSMatthew Dillon strerror(errno));
70*ba1276acSMatthew Dillon #endif
71*ba1276acSMatthew Dillon #ifdef PT_DENY_ATTACH
72*ba1276acSMatthew Dillon /* Mac OS X */
73*ba1276acSMatthew Dillon if (ptrace(PT_DENY_ATTACH, 0, 0, 0) == -1 && strict)
74*ba1276acSMatthew Dillon fatal("unable to set PT_DENY_ATTACH: %s", strerror(errno));
75*ba1276acSMatthew Dillon #endif
76*ba1276acSMatthew Dillon }
77