source: trunk/base/src/darwintracelib1.0/darwintrace.h

Last change on this file was 117751, checked in by cal@…, 7 years ago

darwintrace: use modern interposing, avoid DYLD_FORCE_FLAT_NAMESPACE that's no longer necessary with interposing, #29228, patch by gauravb7090

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Rev URL
File size: 6.8 KB
Line 
1/*
2 * Copyright (c) 2005 Apple Inc. All rights reserved.
3 * Copyright (c) 2005-2006 Paul Guyot <pguyot@kallisys.net>,
4 * All rights reserved.
5 * Copyright (c) 2006-2013 The MacPorts Project
6 *
7 * $Id: darwintrace.h 117751 2014-03-10 21:55:33Z cal@macports.org $
8 *
9 * @APPLE_BSD_LICENSE_HEADER_START@
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1.  Redistributions of source code must retain the above copyright
16 *     notice, this list of conditions and the following disclaimer.
17 * 2.  Redistributions in binary form must reproduce the above copyright
18 *     notice, this list of conditions and the following disclaimer in the
19 *     documentation and/or other materials provided with the distribution.
20 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
21 *     its contributors may be used to endorse or promote products derived
22 *     from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * @APPLE_BSD_LICENSE_HEADER_END@
36 */
37
38#ifdef HAVE_CONFIG_H
39#include <config.h>
40#endif
41
42#include <pthread.h>
43#include <stdbool.h>
44#include <stdio.h>
45
46/**
47 * DARWINTRACE_INTERPOSE: provides a way to override standard library functions
48 * with your own implementations.
49 */
50#ifndef DARWINTRACE_INTERPOSE
51#define DARWINTRACE_INTERPOSE(_replacement, _replacee) \
52__attribute__((used)) static struct { \
53        const void *replacement; \
54        const void *replacee; \
55} _interpose_##_replacee \
56__attribute__((section ("__DATA,__interpose"))) = { \
57        (const void *) (unsigned long) &_replacement, \
58        (const void *) (unsigned long) &_replacee \
59}
60#endif
61
62/**
63 * DARWINTRACE_DEBUG: verbose output of operations to debug darwintrace
64 */
65#ifndef DARWINTRACE_DEBUG
66#define DARWINTRACE_DEBUG (0)
67#endif
68
69/**
70 * \c debug_printf() is a macro that will print a message prefixed with
71 * "darwintrace" and the calling process' PID to stderr, or to the file
72 * indicated by the environment variable \c DARWINTRACE_DEBUG, if set.
73 */
74#if DARWINTRACE_DEBUG
75#       define debug_printf(format, ...) \
76                if (__darwintrace_stderr != NULL) { \
77                        fprintf(__darwintrace_stderr, "darwintrace[%d:%p]: " format, getpid(), (void *) pthread_self(), __VA_ARGS__); \
78                        fflush(__darwintrace_stderr); \
79                }
80#else
81#   define debug_printf(...)
82#endif
83
84enum {
85        DT_REPORT     = 1 << 0,
86        DT_ALLOWDIR   = 1 << 1,
87        DT_FOLLOWSYMS = 1 << 2
88};
89
90/**
91 * Debug socket. Will be set by a constructor function in darwintrace.c.
92 */
93extern FILE *__darwintrace_stderr;
94
95/**
96 * Initializer function, ensures darwintrace has been properly set up and check
97 * whether this process was fork(2)'d or clone(2)'d since the last call. Call
98 * this before calling any other functions from this library.
99 */
100void __darwintrace_setup();
101
102/**
103 * Close the darwintrace socket and set it to \c NULL. Since this uses \c
104 * fclose(3), which internally calls \c close(2), which is intercepted by this
105 * library and this library prevents closing the socket to MacPorts, we use \c
106 * __darwintrace_close_sock to allow closing specific FDs.
107 */
108void __darwintrace_close();
109
110/**
111 * Check a path against the current sandbox
112 *
113 * \param[in] path the path to be checked; not necessarily absolute
114 * \param[in] flags A binary or combination of the following flags:
115 *                  - DT_REPORT: If access to this path is being denied, report
116 *                    it as sandbox violation. Set this for all operations that
117 *                    read file contents or check file attributes. Omit this
118 *                    flag for operations that might only attempt to access
119 *                    a file by chance, such as readdir(3).
120 *                  - DT_ALLOWDIR: Whether to always allow access if the given
121 *                    path references an existing directory. Set this for
122 *                    read operations such as stat(2), omit this for operations
123 *                    that modify directories like rmdir(2) and mkdir(2).
124 *                  - DT_FOLLOWSYMS: Check for and expand symlinks, while
125 *                    checking both the link name and the link target against
126 *                    the sandbox. Set this for all operations that read file
127 *                    contents or check file attributes. Omit this flag for
128 *                    operations that only list the file (or rather symlink)
129 *                    name.
130 * \return \c true if the file is within sandbox bounds, \c false if access
131 *         should be denied
132 */
133bool __darwintrace_is_in_sandbox(const char *path, int flags);
134
135#ifdef DARWINTRACE_USE_PRIVATE_API
136#include <errno.h>
137#include <stdlib.h>
138
139/**
140 * PID of the process darwintrace was last used in. This is used to detect
141 * forking and opening a new connection to the control socket in the child
142 * process. Not doing so would potentially cause two processes writing to the
143 * same socket.
144 */
145extern pid_t __darwintrace_pid;
146
147/**
148 * Copy of the DARWINTRACE_LOG environment variable to restore it in execve(2).
149 * Contains the path to the unix socket used for communication with the
150 * MacPorts-side of the sandbox.
151 */
152extern char *__env_darwintrace_log;
153
154/**
155 * Helper variable containing the number of the darwintrace socket, iff the
156 * close(2) syscall should be allowed to close it. Used by \c
157 * __darwintrace_close.
158 */
159extern volatile int __darwintrace_close_sock;
160
161/**
162 * pthread_key_t for the darwintrace socket to ensure the socket is only used
163 * from a single thread.
164 */
165extern pthread_key_t sock_key;
166
167/**
168 * Convenience getter function for the thread-local darwintrace socket. Do not
169 * consider this part of public API. It is only needed to prevent closing and
170 * duplicating over darwintrace's socket FDs.
171 */
172static inline FILE *__darwintrace_sock() {
173        return (FILE *) pthread_getspecific(sock_key);
174}
175
176/**
177 * Convenience setter function for the thread-local darwintrace socket. Do not
178 * consider this part of public API. It is only needed to prevent closing and
179 * duplicating over darwintrace's socket FDs.
180 */
181static inline void __darwintrace_sock_set(FILE *stream) {
182        if (0 != (errno = pthread_setspecific(sock_key, stream))) {
183                perror("darwintrace: pthread_setspecific");
184                abort();
185        }
186}
187#endif /* defined(DARWINTRACE_USE_PRIVATE_API) */
Note: See TracBrowser for help on using the repository browser.