18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * An access vector table (avtab) is a hash table
48c2ecf20Sopenharmony_ci * of access vectors and transition types indexed
58c2ecf20Sopenharmony_ci * by a type pair and a class.  An access vector
68c2ecf20Sopenharmony_ci * table is used to represent the type enforcement
78c2ecf20Sopenharmony_ci * tables.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *  Author : Stephen Smalley, <sds@tycho.nsa.gov>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/* Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * 	Added conditional policy language extensions
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * Copyright (C) 2003 Tresys Technology, LLC
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * Updated: Yuichi Nakamura <ynakam@hitachisoft.jp>
198c2ecf20Sopenharmony_ci * 	Tuned number of hash slots for avtab to reduce memory usage
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci#ifndef _SS_AVTAB_H_
228c2ecf20Sopenharmony_ci#define _SS_AVTAB_H_
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "security.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct avtab_key {
278c2ecf20Sopenharmony_ci	u16 source_type;	/* source type */
288c2ecf20Sopenharmony_ci	u16 target_type;	/* target type */
298c2ecf20Sopenharmony_ci	u16 target_class;	/* target object class */
308c2ecf20Sopenharmony_ci#define AVTAB_ALLOWED		0x0001
318c2ecf20Sopenharmony_ci#define AVTAB_AUDITALLOW	0x0002
328c2ecf20Sopenharmony_ci#define AVTAB_AUDITDENY		0x0004
338c2ecf20Sopenharmony_ci#define AVTAB_AV		(AVTAB_ALLOWED | AVTAB_AUDITALLOW | AVTAB_AUDITDENY)
348c2ecf20Sopenharmony_ci#define AVTAB_TRANSITION	0x0010
358c2ecf20Sopenharmony_ci#define AVTAB_MEMBER		0x0020
368c2ecf20Sopenharmony_ci#define AVTAB_CHANGE		0x0040
378c2ecf20Sopenharmony_ci#define AVTAB_TYPE		(AVTAB_TRANSITION | AVTAB_MEMBER | AVTAB_CHANGE)
388c2ecf20Sopenharmony_ci/* extended permissions */
398c2ecf20Sopenharmony_ci#define AVTAB_XPERMS_ALLOWED	0x0100
408c2ecf20Sopenharmony_ci#define AVTAB_XPERMS_AUDITALLOW	0x0200
418c2ecf20Sopenharmony_ci#define AVTAB_XPERMS_DONTAUDIT	0x0400
428c2ecf20Sopenharmony_ci#define AVTAB_XPERMS		(AVTAB_XPERMS_ALLOWED | \
438c2ecf20Sopenharmony_ci				AVTAB_XPERMS_AUDITALLOW | \
448c2ecf20Sopenharmony_ci				AVTAB_XPERMS_DONTAUDIT)
458c2ecf20Sopenharmony_ci#define AVTAB_ENABLED_OLD   0x80000000 /* reserved for used in cond_avtab */
468c2ecf20Sopenharmony_ci#define AVTAB_ENABLED		0x8000 /* reserved for used in cond_avtab */
478c2ecf20Sopenharmony_ci	u16 specified;	/* what field is specified */
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/*
518c2ecf20Sopenharmony_ci * For operations that require more than the 32 permissions provided by the avc
528c2ecf20Sopenharmony_ci * extended permissions may be used to provide 256 bits of permissions.
538c2ecf20Sopenharmony_ci */
548c2ecf20Sopenharmony_cistruct avtab_extended_perms {
558c2ecf20Sopenharmony_ci/* These are not flags. All 256 values may be used */
568c2ecf20Sopenharmony_ci#define AVTAB_XPERMS_IOCTLFUNCTION	0x01
578c2ecf20Sopenharmony_ci#define AVTAB_XPERMS_IOCTLDRIVER	0x02
588c2ecf20Sopenharmony_ci	/* extension of the avtab_key specified */
598c2ecf20Sopenharmony_ci	u8 specified; /* ioctl, netfilter, ... */
608c2ecf20Sopenharmony_ci	/*
618c2ecf20Sopenharmony_ci	 * if 256 bits is not adequate as is often the case with ioctls, then
628c2ecf20Sopenharmony_ci	 * multiple extended perms may be used and the driver field
638c2ecf20Sopenharmony_ci	 * specifies which permissions are included.
648c2ecf20Sopenharmony_ci	 */
658c2ecf20Sopenharmony_ci	u8 driver;
668c2ecf20Sopenharmony_ci	/* 256 bits of permissions */
678c2ecf20Sopenharmony_ci	struct extended_perms_data perms;
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistruct avtab_datum {
718c2ecf20Sopenharmony_ci	union {
728c2ecf20Sopenharmony_ci		u32 data; /* access vector or type value */
738c2ecf20Sopenharmony_ci		struct avtab_extended_perms *xperms;
748c2ecf20Sopenharmony_ci	} u;
758c2ecf20Sopenharmony_ci};
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistruct avtab_node {
788c2ecf20Sopenharmony_ci	struct avtab_key key;
798c2ecf20Sopenharmony_ci	struct avtab_datum datum;
808c2ecf20Sopenharmony_ci	struct avtab_node *next;
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistruct avtab {
848c2ecf20Sopenharmony_ci	struct avtab_node **htable;
858c2ecf20Sopenharmony_ci	u32 nel;	/* number of elements */
868c2ecf20Sopenharmony_ci	u32 nslot;      /* number of hash slots */
878c2ecf20Sopenharmony_ci	u32 mask;       /* mask to compute hash func */
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_civoid avtab_init(struct avtab *h);
918c2ecf20Sopenharmony_ciint avtab_alloc(struct avtab *, u32);
928c2ecf20Sopenharmony_ciint avtab_alloc_dup(struct avtab *new, const struct avtab *orig);
938c2ecf20Sopenharmony_cistruct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *k);
948c2ecf20Sopenharmony_civoid avtab_destroy(struct avtab *h);
958c2ecf20Sopenharmony_civoid avtab_hash_eval(struct avtab *h, char *tag);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistruct policydb;
988c2ecf20Sopenharmony_ciint avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
998c2ecf20Sopenharmony_ci		    int (*insert)(struct avtab *a, struct avtab_key *k,
1008c2ecf20Sopenharmony_ci				  struct avtab_datum *d, void *p),
1018c2ecf20Sopenharmony_ci		    void *p);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciint avtab_read(struct avtab *a, void *fp, struct policydb *pol);
1048c2ecf20Sopenharmony_ciint avtab_write_item(struct policydb *p, struct avtab_node *cur, void *fp);
1058c2ecf20Sopenharmony_ciint avtab_write(struct policydb *p, struct avtab *a, void *fp);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistruct avtab_node *avtab_insert_nonunique(struct avtab *h, struct avtab_key *key,
1088c2ecf20Sopenharmony_ci					  struct avtab_datum *datum);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistruct avtab_node *avtab_search_node(struct avtab *h, struct avtab_key *key);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistruct avtab_node *avtab_search_node_next(struct avtab_node *node, int specified);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define MAX_AVTAB_HASH_BITS 16
1158c2ecf20Sopenharmony_ci#define MAX_AVTAB_HASH_BUCKETS (1 << MAX_AVTAB_HASH_BITS)
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#endif	/* _SS_AVTAB_H_ */
1188c2ecf20Sopenharmony_ci
119