1076ad2f8SDag-Erling Smørgrav /*
2076ad2f8SDag-Erling Smørgrav * Copyright (c) 2016 Darren Tucker. All rights reserved.
3076ad2f8SDag-Erling Smørgrav *
4076ad2f8SDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any
5076ad2f8SDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above
6076ad2f8SDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies.
7076ad2f8SDag-Erling Smørgrav *
8076ad2f8SDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9076ad2f8SDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10076ad2f8SDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11076ad2f8SDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12076ad2f8SDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13076ad2f8SDag-Erling Smørgrav * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14076ad2f8SDag-Erling Smørgrav * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15076ad2f8SDag-Erling Smørgrav */
16076ad2f8SDag-Erling Smørgrav
17076ad2f8SDag-Erling Smørgrav #include "includes.h"
18076ad2f8SDag-Erling Smørgrav
19076ad2f8SDag-Erling Smørgrav #include <sys/types.h>
20e9e8876aSEd Maste #ifdef HAVE_SYS_PROCCTL_H
21e9e8876aSEd Maste #include <sys/procctl.h>
22e9e8876aSEd Maste #endif
23076ad2f8SDag-Erling Smørgrav #if defined(HAVE_SYS_PRCTL_H)
24076ad2f8SDag-Erling Smørgrav #include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
25076ad2f8SDag-Erling Smørgrav #endif
26ca86bcf2SDag-Erling Smørgrav #ifdef HAVE_SYS_PTRACE_H
27ca86bcf2SDag-Erling Smørgrav #include <sys/ptrace.h>
28ca86bcf2SDag-Erling Smørgrav #endif
29076ad2f8SDag-Erling Smørgrav #ifdef HAVE_PRIV_H
30076ad2f8SDag-Erling Smørgrav #include <priv.h> /* For setpflags() and __PROC_PROTECT */
31076ad2f8SDag-Erling Smørgrav #endif
32076ad2f8SDag-Erling Smørgrav #include <stdarg.h>
331323ec57SEd Maste #include <stdio.h>
341323ec57SEd Maste #include <string.h>
354232f36eSEd Maste #include <unistd.h>
36076ad2f8SDag-Erling Smørgrav
37076ad2f8SDag-Erling Smørgrav #include "log.h"
38076ad2f8SDag-Erling Smørgrav
39076ad2f8SDag-Erling Smørgrav void
platform_disable_tracing(int strict)40076ad2f8SDag-Erling Smørgrav platform_disable_tracing(int strict)
41076ad2f8SDag-Erling Smørgrav {
42e9e8876aSEd Maste #if defined(HAVE_PROCCTL) && defined(PROC_TRACE_CTL)
43e9e8876aSEd Maste /* On FreeBSD, we should make this process untraceable */
44e9e8876aSEd Maste int disable_trace = PROC_TRACE_CTL_DISABLE;
45e9e8876aSEd Maste
464232f36eSEd Maste /*
474232f36eSEd Maste * On FreeBSD, we should make this process untraceable.
48*733bf3b1SEd Maste * pid=0 means "this process" but some older kernels do not
49*733bf3b1SEd Maste * understand that so retry with our own pid before failing.
504232f36eSEd Maste */
514232f36eSEd Maste if (procctl(P_PID, 0, PROC_TRACE_CTL, &disable_trace) == 0)
524232f36eSEd Maste return;
534232f36eSEd Maste if (procctl(P_PID, getpid(), PROC_TRACE_CTL, &disable_trace) == 0)
544232f36eSEd Maste return;
554232f36eSEd Maste if (strict)
561323ec57SEd Maste fatal("unable to make the process untraceable: %s",
571323ec57SEd Maste strerror(errno));
58e9e8876aSEd Maste #endif
59076ad2f8SDag-Erling Smørgrav #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
60076ad2f8SDag-Erling Smørgrav /* Disable ptrace on Linux without sgid bit */
61076ad2f8SDag-Erling Smørgrav if (prctl(PR_SET_DUMPABLE, 0) != 0 && strict)
621323ec57SEd Maste fatal("unable to make the process undumpable: %s",
631323ec57SEd Maste strerror(errno));
64076ad2f8SDag-Erling Smørgrav #endif
65076ad2f8SDag-Erling Smørgrav #if defined(HAVE_SETPFLAGS) && defined(__PROC_PROTECT)
66076ad2f8SDag-Erling Smørgrav /* On Solaris, we should make this process untraceable */
67076ad2f8SDag-Erling Smørgrav if (setpflags(__PROC_PROTECT, 1) != 0 && strict)
681323ec57SEd Maste fatal("unable to make the process untraceable: %s",
691323ec57SEd Maste strerror(errno));
70076ad2f8SDag-Erling Smørgrav #endif
71ca86bcf2SDag-Erling Smørgrav #ifdef PT_DENY_ATTACH
72ca86bcf2SDag-Erling Smørgrav /* Mac OS X */
73ca86bcf2SDag-Erling Smørgrav if (ptrace(PT_DENY_ATTACH, 0, 0, 0) == -1 && strict)
741323ec57SEd Maste fatal("unable to set PT_DENY_ATTACH: %s", strerror(errno));
75ca86bcf2SDag-Erling Smørgrav #endif
76076ad2f8SDag-Erling Smørgrav }
77