2013-06-17 12:07:14 +02:00
|
|
|
/* Copyright (c) 2001 Matej Pfajfar.
|
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
|
|
|
* Copyright (c) 2007-2013, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file sandbox.h
|
|
|
|
* \brief Header file for sandbox.c.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#ifndef SANDBOX_H_
|
|
|
|
#define SANDBOX_H_
|
|
|
|
|
2013-09-09 21:59:41 +02:00
|
|
|
#include "orconfig.h"
|
|
|
|
#include "torint.h"
|
|
|
|
|
2013-06-17 12:07:14 +02:00
|
|
|
#ifndef SYS_SECCOMP
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by SIGSYS signal handler to check if the signal was issued due to a
|
|
|
|
* seccomp2 filter violation.
|
|
|
|
*/
|
|
|
|
#define SYS_SECCOMP 1
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-09-09 21:59:41 +02:00
|
|
|
#if defined(HAVE_SECCOMP_H) && defined(__linux__)
|
|
|
|
#define USE_LIBSECCOMP
|
|
|
|
#endif
|
|
|
|
|
2013-09-11 19:53:26 +02:00
|
|
|
struct sandbox_cfg_elem;
|
|
|
|
|
|
|
|
/** Typedef to structure used to manage a sandbox configuration. */
|
|
|
|
typedef struct sandbox_cfg_elem sandbox_cfg_t;
|
|
|
|
|
2013-06-17 12:07:14 +02:00
|
|
|
/**
|
|
|
|
* Linux definitions
|
|
|
|
*/
|
2013-09-09 21:59:41 +02:00
|
|
|
#ifdef USE_LIBSECCOMP
|
2013-06-17 12:07:14 +02:00
|
|
|
|
2013-07-29 15:30:39 +02:00
|
|
|
#ifndef __USE_GNU
|
2013-06-17 12:07:14 +02:00
|
|
|
#define __USE_GNU
|
2013-07-29 15:30:39 +02:00
|
|
|
#endif
|
2013-06-17 12:07:14 +02:00
|
|
|
#include <sys/ucontext.h>
|
2013-07-30 20:23:30 +02:00
|
|
|
#include <seccomp.h>
|
2013-08-19 12:56:50 +02:00
|
|
|
#include <netdb.h>
|
2013-06-17 12:07:14 +02:00
|
|
|
|
2013-07-23 09:49:56 +02:00
|
|
|
#define PARAM_PTR 0
|
|
|
|
#define PARAM_NUM 1
|
|
|
|
|
2013-08-14 23:09:07 +02:00
|
|
|
/**
|
2013-09-02 12:54:43 +02:00
|
|
|
* Enum used to manage the type of the implementation for general purpose.
|
2013-08-14 23:09:07 +02:00
|
|
|
*/
|
2013-09-02 12:54:43 +02:00
|
|
|
typedef enum {
|
|
|
|
/** Libseccomp implementation based on seccomp2*/
|
|
|
|
LIBSECCOMP2 = 0
|
|
|
|
} SB_IMPL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration parameter structure associated with the LIBSECCOMP2
|
|
|
|
* implementation.
|
|
|
|
*/
|
|
|
|
typedef struct smp_param {
|
2013-08-28 19:01:52 +02:00
|
|
|
/** syscall associated with parameter. */
|
|
|
|
int syscall;
|
2013-07-24 16:06:06 +02:00
|
|
|
|
2013-08-28 19:01:52 +02:00
|
|
|
/** parameter value. */
|
2013-09-02 12:54:43 +02:00
|
|
|
intptr_t value;
|
2014-03-28 08:51:50 +01:00
|
|
|
/** parameter value, second argument. */
|
|
|
|
intptr_t value2;
|
2013-07-24 16:06:06 +02:00
|
|
|
|
2013-08-28 19:01:52 +02:00
|
|
|
/** parameter flag (0 = not protected, 1 = protected). */
|
2013-08-29 14:19:49 +02:00
|
|
|
int prot;
|
2013-09-02 12:54:43 +02:00
|
|
|
} smp_param_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Structure used to manage a sandbox configuration.
|
|
|
|
*
|
|
|
|
* It is implemented as a linked list of parameters. Currently only controls
|
|
|
|
* parameters for open, openat, execve, stat64.
|
|
|
|
*/
|
|
|
|
struct sandbox_cfg_elem {
|
|
|
|
/** Sandbox implementation which dictates the parameter type. */
|
|
|
|
SB_IMPL implem;
|
|
|
|
|
|
|
|
/** Configuration parameter. */
|
2014-04-16 18:24:08 +02:00
|
|
|
smp_param_t *param;
|
2013-07-24 16:06:06 +02:00
|
|
|
|
2013-09-02 12:54:43 +02:00
|
|
|
/** Next element of the configuration*/
|
|
|
|
struct sandbox_cfg_elem *next;
|
2013-07-24 16:06:06 +02:00
|
|
|
};
|
2013-07-18 17:03:10 +02:00
|
|
|
|
2013-08-14 23:09:07 +02:00
|
|
|
/** Function pointer defining the prototype of a filter function.*/
|
2013-08-05 14:40:23 +02:00
|
|
|
typedef int (*sandbox_filter_func_t)(scmp_filter_ctx ctx,
|
|
|
|
sandbox_cfg_t *filter);
|
2013-07-30 20:23:30 +02:00
|
|
|
|
2013-08-14 23:09:07 +02:00
|
|
|
/** Type that will be used in step 3 in order to manage multiple sandboxes.*/
|
2013-08-05 15:01:31 +02:00
|
|
|
typedef struct {
|
2013-08-29 11:41:17 +02:00
|
|
|
/** function pointers associated with the filter */
|
2013-08-05 15:01:31 +02:00
|
|
|
sandbox_filter_func_t *filter_func;
|
|
|
|
|
2013-08-29 11:41:17 +02:00
|
|
|
/** filter function pointer parameters */
|
2013-08-05 15:01:31 +02:00
|
|
|
sandbox_cfg_t *filter_dynamic;
|
|
|
|
} sandbox_t;
|
|
|
|
|
2013-09-09 21:59:41 +02:00
|
|
|
#endif // USE_LIBSECCOMP
|
|
|
|
|
|
|
|
#ifdef USE_LIBSECCOMP
|
2013-09-02 10:44:04 +02:00
|
|
|
/** Pre-calls getaddrinfo in order to pre-record result. */
|
|
|
|
int sandbox_add_addrinfo(const char *addr);
|
|
|
|
|
2013-09-09 21:59:41 +02:00
|
|
|
struct addrinfo;
|
2013-08-19 12:56:50 +02:00
|
|
|
/** Replacement for getaddrinfo(), using pre-recorded results. */
|
2013-09-09 21:59:41 +02:00
|
|
|
int sandbox_getaddrinfo(const char *name, const char *servname,
|
|
|
|
const struct addrinfo *hints,
|
|
|
|
struct addrinfo **res);
|
2014-05-22 23:39:36 +02:00
|
|
|
#define sandbox_freeaddrinfo(addrinfo) ((void)0)
|
|
|
|
void sandbox_free_getaddrinfo_cache(void);
|
2013-09-09 21:59:41 +02:00
|
|
|
#else
|
|
|
|
#define sandbox_getaddrinfo(name, servname, hints, res) \
|
|
|
|
getaddrinfo((name),(servname), (hints),(res))
|
|
|
|
#define sandbox_add_addrinfo(name) \
|
|
|
|
((void)(name))
|
2014-05-22 23:39:36 +02:00
|
|
|
#define sandbox_freeaddrinfo(addrinfo) \
|
|
|
|
freeaddrinfo((addrinfo))
|
|
|
|
#define sandbox_free_getaddrinfo_cache()
|
2013-09-09 21:59:41 +02:00
|
|
|
#endif
|
2013-08-19 12:56:50 +02:00
|
|
|
|
2013-09-09 21:59:41 +02:00
|
|
|
#ifdef USE_LIBSECCOMP
|
2013-08-14 23:09:07 +02:00
|
|
|
/** Returns a registered protected string used with the sandbox, given that
|
|
|
|
* it matches the parameter.
|
|
|
|
*/
|
2013-07-29 15:30:39 +02:00
|
|
|
const char* sandbox_intern_string(const char *param);
|
2013-09-09 21:59:41 +02:00
|
|
|
#else
|
|
|
|
#define sandbox_intern_string(s) (s)
|
|
|
|
#endif
|
2013-07-25 13:08:02 +02:00
|
|
|
|
2013-08-14 23:09:07 +02:00
|
|
|
/** Creates an empty sandbox configuration file.*/
|
2013-08-28 19:01:52 +02:00
|
|
|
sandbox_cfg_t * sandbox_cfg_new(void);
|
2013-08-10 17:04:48 +02:00
|
|
|
|
2013-08-14 23:09:07 +02:00
|
|
|
/**
|
|
|
|
* Function used to add a open allowed filename to a supplied configuration.
|
2014-04-11 09:04:16 +02:00
|
|
|
* The (char*) specifies the path to the allowed file; we take ownership
|
|
|
|
* of the pointer.
|
2013-08-14 23:09:07 +02:00
|
|
|
*/
|
2014-04-11 09:04:16 +02:00
|
|
|
int sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file);
|
2013-08-14 23:09:07 +02:00
|
|
|
|
2014-03-28 08:51:50 +01:00
|
|
|
/**DOCDOC*/
|
|
|
|
int sandbox_cfg_allow_rename(sandbox_cfg_t **cfg, char *file1, char *file2);
|
|
|
|
|
2013-08-14 23:09:07 +02:00
|
|
|
/** Function used to add a series of open allowed filenames to a supplied
|
|
|
|
* configuration.
|
|
|
|
* @param cfg sandbox configuration.
|
2014-04-11 09:04:16 +02:00
|
|
|
* @param ... a list of stealable pointers to permitted files. The last
|
|
|
|
* one must be NULL.
|
|
|
|
*/
|
2013-08-29 14:42:30 +02:00
|
|
|
int sandbox_cfg_allow_open_filename_array(sandbox_cfg_t **cfg, ...);
|
2013-08-10 17:04:48 +02:00
|
|
|
|
2013-08-14 23:09:07 +02:00
|
|
|
/**
|
|
|
|
* Function used to add a openat allowed filename to a supplied configuration.
|
2014-04-11 09:04:16 +02:00
|
|
|
* The (char*) specifies the path to the allowed file; we steal the pointer to
|
|
|
|
* that file.
|
2013-08-14 23:09:07 +02:00
|
|
|
*/
|
2014-04-11 09:04:16 +02:00
|
|
|
int sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file);
|
2013-08-14 23:09:07 +02:00
|
|
|
|
|
|
|
/** Function used to add a series of openat allowed filenames to a supplied
|
|
|
|
* configuration.
|
|
|
|
* @param cfg sandbox configuration.
|
2014-04-11 09:04:16 +02:00
|
|
|
* @param ... a list of stealable pointers to permitted files. The last
|
|
|
|
* one must be NULL.
|
2013-08-14 23:09:07 +02:00
|
|
|
*/
|
2013-08-29 14:42:30 +02:00
|
|
|
int sandbox_cfg_allow_openat_filename_array(sandbox_cfg_t **cfg, ...);
|
2013-08-10 17:04:48 +02:00
|
|
|
|
2014-05-20 18:21:31 +02:00
|
|
|
#if 0
|
2013-08-14 23:09:07 +02:00
|
|
|
/**
|
|
|
|
* Function used to add a execve allowed filename to a supplied configuration.
|
2014-04-11 09:04:16 +02:00
|
|
|
* The (char*) specifies the path to the allowed file; that pointer is stolen.
|
2013-08-14 23:09:07 +02:00
|
|
|
*/
|
2013-09-09 21:14:50 +02:00
|
|
|
int sandbox_cfg_allow_execve(sandbox_cfg_t **cfg, const char *com);
|
2013-08-14 23:09:07 +02:00
|
|
|
|
|
|
|
/** Function used to add a series of execve allowed filenames to a supplied
|
|
|
|
* configuration.
|
|
|
|
* @param cfg sandbox configuration.
|
2014-04-11 09:04:16 +02:00
|
|
|
* @param ... an array of stealable pointers to permitted files. The last
|
|
|
|
* one must be NULL.
|
2013-08-14 23:09:07 +02:00
|
|
|
*/
|
2013-08-29 14:42:30 +02:00
|
|
|
int sandbox_cfg_allow_execve_array(sandbox_cfg_t **cfg, ...);
|
2014-05-20 18:21:31 +02:00
|
|
|
#endif
|
2013-08-10 17:04:48 +02:00
|
|
|
|
2013-08-14 23:09:07 +02:00
|
|
|
/**
|
2013-09-09 20:55:47 +02:00
|
|
|
* Function used to add a stat/stat64 allowed filename to a configuration.
|
2014-04-11 09:04:16 +02:00
|
|
|
* The (char*) specifies the path to the allowed file; that pointer is stolen.
|
2013-08-14 23:09:07 +02:00
|
|
|
*/
|
2014-04-11 09:04:16 +02:00
|
|
|
int sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file);
|
2013-08-14 23:09:07 +02:00
|
|
|
|
|
|
|
/** Function used to add a series of stat64 allowed filenames to a supplied
|
|
|
|
* configuration.
|
|
|
|
* @param cfg sandbox configuration.
|
2014-04-11 09:04:16 +02:00
|
|
|
* @param ... an array of stealable pointers to permitted files. The last
|
|
|
|
* one must be NULL.
|
2013-08-14 23:09:07 +02:00
|
|
|
*/
|
2013-09-09 20:55:47 +02:00
|
|
|
int sandbox_cfg_allow_stat_filename_array(sandbox_cfg_t **cfg, ...);
|
2013-08-12 20:14:43 +02:00
|
|
|
|
2013-08-14 23:09:07 +02:00
|
|
|
/** Function used to initialise a sandbox configuration.*/
|
2013-07-25 13:08:02 +02:00
|
|
|
int sandbox_init(sandbox_cfg_t* cfg);
|
2013-06-17 12:07:14 +02:00
|
|
|
|
2014-04-16 22:05:10 +02:00
|
|
|
/** Return true iff the sandbox is turned on. */
|
|
|
|
int sandbox_is_active(void);
|
|
|
|
|
2013-06-17 12:07:14 +02:00
|
|
|
#endif /* SANDBOX_H_ */
|
|
|
|
|