1/*
2 * $FreeBSD$
3 *
4 * Copyright (c) 2011, 2012, 2013, 2015, 2016, Juniper Networks, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#ifndef	_SECURITY_MAC_VERIEXEC_H
30#define	_SECURITY_MAC_VERIEXEC_H
31
32#ifdef _KERNEL
33#include <sys/types.h>
34#include <sys/kernel.h>
35#include <sys/queue.h>
36#include <sys/module.h>
37#endif
38
39/**
40 * Name of the MAC module
41 */
42#define	MAC_VERIEXEC_NAME	"mac_veriexec"
43
44/* MAC/veriexec syscalls */
45#define	MAC_VERIEXEC_CHECK_FD_SYSCALL	1
46#define	MAC_VERIEXEC_CHECK_PATH_SYSCALL	2
47
48/**
49 * Enough room for the largest signature...
50 */
51#define MAXFINGERPRINTLEN	64	/* enough room for largest signature */
52
53/*
54 * Types of veriexec inodes we can have
55 */
56#define VERIEXEC_INDIRECT	(1<<0)  /* Only allow indirect execution */
57#define VERIEXEC_FILE		(1<<1)  /* Fingerprint of a plain file */
58#define VERIEXEC_NOTRACE	(1<<2)	/**< PTRACE not allowed */
59#define VERIEXEC_TRUSTED	(1<<3)	/**< Safe to write /dev/mem */
60/* XXX these are currently unimplemented */
61#define VERIEXEC_NOFIPS		(1<<4)	/**< Not allowed in FIPS mode */
62
63#define VERIEXEC_STATE_INACTIVE	0	/**< Ignore */
64#define VERIEXEC_STATE_LOADED	(1<<0)	/**< Sigs have been loaded */
65#define VERIEXEC_STATE_ACTIVE	(1<<1)	/**< Pay attention to it */
66#define VERIEXEC_STATE_ENFORCE	(1<<2)	/**< Fail execs for files that do not
67					     match signature */
68#define VERIEXEC_STATE_LOCKED	(1<<3)	/**< Do not allow further changes */
69
70#ifdef _KERNEL
71/**
72 * Version of the MAC/veriexec module
73 */
74#define	MAC_VERIEXEC_VERSION	1
75
76/* Valid states for the fingerprint flag - if signed exec is being used */
77typedef enum fingerprint_status {
78	FINGERPRINT_INVALID,	/**< Fingerprint has not been evaluated */
79	FINGERPRINT_VALID,	/**< Fingerprint evaluated and matches list */
80	FINGERPRINT_INDIRECT,	/**< Fingerprint eval'd/matched but only
81				     indirect execs allowed */
82	FINGERPRINT_FILE,	/**< Fingerprint evaluated/matched but
83				     not executable */
84	FINGERPRINT_NOMATCH,	/**< Fingerprint evaluated but does not match */
85	FINGERPRINT_NOENTRY,	/**< Fingerprint evaluated but no list entry */
86	FINGERPRINT_NODEV,	/**< Fingerprint evaluated but no dev list */
87} fingerprint_status_t;
88
89typedef void (*mac_veriexec_fpop_init_t)(void *);
90typedef void (*mac_veriexec_fpop_update_t)(void *, const uint8_t *, size_t);
91typedef void (*mac_veriexec_fpop_final_t)(uint8_t *, void *);
92
93struct mac_veriexec_fpops {
94	const char *type;
95	size_t digest_len;
96	size_t context_size;
97	mac_veriexec_fpop_init_t init;
98	mac_veriexec_fpop_update_t update;
99	mac_veriexec_fpop_final_t final;
100	LIST_ENTRY(mac_veriexec_fpops) entries;
101};
102
103/**
104 * Verified execution subsystem debugging level
105 */
106extern int	mac_veriexec_debug;
107
108/**
109 * @brief Define a fingerprint module.
110 *
111 * @param _name		Name of the fingerprint module
112 * @param _digest_len	Length of the digest string, in number of characters
113 * @param _context_size	Size of the context structure, in bytes
114 * @param _init		Initialization function of type
115 * 			mac_veriexec_fpop_init_t
116 * @param _update	Update function of type mac_veriexec_fpop_update_t
117 * @param _final	Finalize function of type mac_veriexec_fpop_final_t
118 * @param _vers		Module version
119 */
120#define MAC_VERIEXEC_FPMOD(_name, _digest_len, _context_size, _init,	\
121	    _update, _final, _vers)					\
122	static struct mac_veriexec_fpops				\
123	    mac_veriexec_##_name##_fpops = {				\
124		.type = #_name,						\
125		.digest_len = _digest_len,				\
126		.context_size = _context_size,				\
127		.init = _init,						\
128		.update = _update,					\
129		.final = _final,					\
130	};								\
131	static moduledata_t mac_veriexec_##_name##_mod = {		\
132		"mac_veriexec/" #_name,					\
133		mac_veriexec_fingerprint_modevent,			\
134		&(mac_veriexec_##_name##_fpops)				\
135	};								\
136	MODULE_VERSION(mac_veriexec_##_name, _vers);			\
137	DECLARE_MODULE(mac_veriexec_##_name,				\
138	    mac_veriexec_##_name##_mod, SI_SUB_MAC_POLICY,		\
139	    SI_ORDER_ANY);						\
140	MODULE_DEPEND(mac_veriexec_##_name, mac_veriexec,		\
141	    MAC_VERIEXEC_VERSION, MAC_VERIEXEC_VERSION,			\
142	    MAC_VERIEXEC_VERSION)
143
144/*
145 * The following function should not be called directly. The prototype is
146 * included here to satisfy the compiler when using the macro above.
147 */
148int	mac_veriexec_fingerprint_modevent(module_t mod, int type, void *data);
149
150/*
151 * Public functions
152 */
153int	mac_veriexec_metadata_add_file(int file_dev, dev_t fsid, long fileid,
154	    unsigned long gen, unsigned char fingerprint[MAXFINGERPRINTLEN],
155	    int flags, const char *fp_type, int override);
156int	mac_veriexec_metadata_has_file(dev_t fsid, long fileid,
157	    unsigned long gen);
158int	mac_veriexec_proc_is_trusted(struct ucred *cred, struct proc *p);
159#endif
160
161#endif	/* _SECURITY_MAC_VERIEXEC_H */
162