1a10e763bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * An access vector table (avtab) is a hash table
41da177e4SLinus Torvalds * of access vectors and transition types indexed
51da177e4SLinus Torvalds * by a type pair and a class. An access vector
61da177e4SLinus Torvalds * table is used to represent the type enforcement
71da177e4SLinus Torvalds * tables.
81da177e4SLinus Torvalds *
90fe53224SStephen Smalley * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
101da177e4SLinus Torvalds */
111da177e4SLinus Torvalds
121da177e4SLinus Torvalds /* Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
131da177e4SLinus Torvalds *
141da177e4SLinus Torvalds * Added conditional policy language extensions
151da177e4SLinus Torvalds *
161da177e4SLinus Torvalds * Copyright (C) 2003 Tresys Technology, LLC
173232c110SYuichi Nakamura *
183232c110SYuichi Nakamura * Updated: Yuichi Nakamura <ynakam@hitachisoft.jp>
193232c110SYuichi Nakamura * Tuned number of hash slots for avtab to reduce memory usage
201da177e4SLinus Torvalds */
211da177e4SLinus Torvalds #ifndef _SS_AVTAB_H_
221da177e4SLinus Torvalds #define _SS_AVTAB_H_
231da177e4SLinus Torvalds
24fa1aa143SJeff Vander Stoep #include "security.h"
25ba39db6eSStephen Smalley
261da177e4SLinus Torvalds struct avtab_key {
27782ebb99SStephen Smalley u16 source_type; /* source type */
28782ebb99SStephen Smalley u16 target_type; /* target type */
29782ebb99SStephen Smalley u16 target_class; /* target object class */
30652bb9b0SEric Paris #define AVTAB_ALLOWED 0x0001
31652bb9b0SEric Paris #define AVTAB_AUDITALLOW 0x0002
32652bb9b0SEric Paris #define AVTAB_AUDITDENY 0x0004
331da177e4SLinus Torvalds #define AVTAB_AV (AVTAB_ALLOWED | AVTAB_AUDITALLOW | AVTAB_AUDITDENY)
34652bb9b0SEric Paris #define AVTAB_TRANSITION 0x0010
35652bb9b0SEric Paris #define AVTAB_MEMBER 0x0020
36652bb9b0SEric Paris #define AVTAB_CHANGE 0x0040
371da177e4SLinus Torvalds #define AVTAB_TYPE (AVTAB_TRANSITION | AVTAB_MEMBER | AVTAB_CHANGE)
38fa1aa143SJeff Vander Stoep /* extended permissions */
39fa1aa143SJeff Vander Stoep #define AVTAB_XPERMS_ALLOWED 0x0100
40fa1aa143SJeff Vander Stoep #define AVTAB_XPERMS_AUDITALLOW 0x0200
41fa1aa143SJeff Vander Stoep #define AVTAB_XPERMS_DONTAUDIT 0x0400
42fa1aa143SJeff Vander Stoep #define AVTAB_XPERMS (AVTAB_XPERMS_ALLOWED | \
43fa1aa143SJeff Vander Stoep AVTAB_XPERMS_AUDITALLOW | \
44fa1aa143SJeff Vander Stoep AVTAB_XPERMS_DONTAUDIT)
45782ebb99SStephen Smalley #define AVTAB_ENABLED_OLD 0x80000000 /* reserved for used in cond_avtab */
46782ebb99SStephen Smalley #define AVTAB_ENABLED 0x8000 /* reserved for used in cond_avtab */
47782ebb99SStephen Smalley u16 specified; /* what field is specified */
48782ebb99SStephen Smalley };
49782ebb99SStephen Smalley
50fa1aa143SJeff Vander Stoep /*
51fa1aa143SJeff Vander Stoep * For operations that require more than the 32 permissions provided by the avc
52fa1aa143SJeff Vander Stoep * extended permissions may be used to provide 256 bits of permissions.
53fa1aa143SJeff Vander Stoep */
54fa1aa143SJeff Vander Stoep struct avtab_extended_perms {
55fa1aa143SJeff Vander Stoep /* These are not flags. All 256 values may be used */
56fa1aa143SJeff Vander Stoep #define AVTAB_XPERMS_IOCTLFUNCTION 0x01
57fa1aa143SJeff Vander Stoep #define AVTAB_XPERMS_IOCTLDRIVER 0x02
58fa1aa143SJeff Vander Stoep /* extension of the avtab_key specified */
59fa1aa143SJeff Vander Stoep u8 specified; /* ioctl, netfilter, ... */
60fa1aa143SJeff Vander Stoep /*
61fa1aa143SJeff Vander Stoep * if 256 bits is not adequate as is often the case with ioctls, then
62fa1aa143SJeff Vander Stoep * multiple extended perms may be used and the driver field
63fa1aa143SJeff Vander Stoep * specifies which permissions are included.
64fa1aa143SJeff Vander Stoep */
65fa1aa143SJeff Vander Stoep u8 driver;
66fa1aa143SJeff Vander Stoep /* 256 bits of permissions */
67fa1aa143SJeff Vander Stoep struct extended_perms_data perms;
68fa1aa143SJeff Vander Stoep };
69fa1aa143SJeff Vander Stoep
70782ebb99SStephen Smalley struct avtab_datum {
71fa1aa143SJeff Vander Stoep union {
72782ebb99SStephen Smalley u32 data; /* access vector or type value */
73fa1aa143SJeff Vander Stoep struct avtab_extended_perms *xperms;
74fa1aa143SJeff Vander Stoep } u;
751da177e4SLinus Torvalds };
761da177e4SLinus Torvalds
771da177e4SLinus Torvalds struct avtab_node {
781da177e4SLinus Torvalds struct avtab_key key;
791da177e4SLinus Torvalds struct avtab_datum datum;
801da177e4SLinus Torvalds struct avtab_node *next;
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds
831da177e4SLinus Torvalds struct avtab {
84acdf52d9SKent Overstreet struct avtab_node **htable;
851da177e4SLinus Torvalds u32 nel; /* number of elements */
863232c110SYuichi Nakamura u32 nslot; /* number of hash slots */
8733ebc193SJohn Brooks u32 mask; /* mask to compute hash func */
881da177e4SLinus Torvalds };
891da177e4SLinus Torvalds
905e729e11SPaul Moore void avtab_init(struct avtab *h);
913232c110SYuichi Nakamura int avtab_alloc(struct avtab *, u32);
92d8f5f0eaSOndrej Mosnacek int avtab_alloc_dup(struct avtab *new, const struct avtab *orig);
931da177e4SLinus Torvalds void avtab_destroy(struct avtab *h);
94*f01dd590SChristian Göttsche
95*f01dd590SChristian Göttsche #ifdef CONFIG_SECURITY_SELINUX_DEBUG
964595ae8cSChristian Göttsche void avtab_hash_eval(struct avtab *h, const char *tag);
97*f01dd590SChristian Göttsche #else
avtab_hash_eval(struct avtab * h,const char * tag)98*f01dd590SChristian Göttsche static inline void avtab_hash_eval(struct avtab *h, const char *tag)
99*f01dd590SChristian Göttsche {
100*f01dd590SChristian Göttsche }
101*f01dd590SChristian Göttsche #endif
1021da177e4SLinus Torvalds
10345e5421eSStephen Smalley struct policydb;
10445e5421eSStephen Smalley int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
105e1cce3a3SOndrej Mosnacek int (*insert)(struct avtab *a, const struct avtab_key *k,
106e1cce3a3SOndrej Mosnacek const struct avtab_datum *d, void *p),
107782ebb99SStephen Smalley void *p);
108782ebb99SStephen Smalley
10945e5421eSStephen Smalley int avtab_read(struct avtab *a, void *fp, struct policydb *pol);
110e1cce3a3SOndrej Mosnacek int avtab_write_item(struct policydb *p, const struct avtab_node *cur, void *fp);
111cee74f47SEric Paris int avtab_write(struct policydb *p, struct avtab *a, void *fp);
1121da177e4SLinus Torvalds
113e1cce3a3SOndrej Mosnacek struct avtab_node *avtab_insert_nonunique(struct avtab *h,
114e1cce3a3SOndrej Mosnacek const struct avtab_key *key,
115e1cce3a3SOndrej Mosnacek const struct avtab_datum *datum);
1161da177e4SLinus Torvalds
117e1cce3a3SOndrej Mosnacek struct avtab_node *avtab_search_node(struct avtab *h,
118e1cce3a3SOndrej Mosnacek const struct avtab_key *key);
1191da177e4SLinus Torvalds
1207128578cSChristian Göttsche struct avtab_node *avtab_search_node_next(struct avtab_node *node, u16 specified);
1211da177e4SLinus Torvalds
122cf7b6c02SStephen Smalley #define MAX_AVTAB_HASH_BITS 16
1233232c110SYuichi Nakamura #define MAX_AVTAB_HASH_BUCKETS (1 << MAX_AVTAB_HASH_BITS)
1241da177e4SLinus Torvalds
1251da177e4SLinus Torvalds #endif /* _SS_AVTAB_H_ */
1261da177e4SLinus Torvalds
127