Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Implementation of the policy database.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Author : Stephen Smalley, <sds@tycho.nsa.gov>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *	Support for enhanced MLS infrastructure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *	Added conditional policy language extensions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * Updated: Hewlett-Packard <paul@paul-moore.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *      Added support for the policy capability bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * Update: Mellanox Techonologies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  *	Added Infiniband support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  * Copyright (C) 2016 Mellanox Techonologies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  * Copyright (C) 2003 - 2004 Tresys Technology, LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/audit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include "security.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include "policydb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include "conditional.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include "mls.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include "services.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #define _DEBUG_HASHES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #ifdef DEBUG_HASHES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) static const char *symtab_name[SYM_NUM] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	"common prefixes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	"classes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	"roles",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	"types",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	"users",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	"bools",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	"levels",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	"categories",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) struct policydb_compat_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	int version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	int sym_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	int ocon_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) /* These need to be updated if SYM_NUM or OCON_NUM changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static struct policydb_compat_info policydb_compat[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 		.version	= POLICYDB_VERSION_BASE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		.sym_num	= SYM_NUM - 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 		.ocon_num	= OCON_NUM - 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 		.version	= POLICYDB_VERSION_BOOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 		.sym_num	= SYM_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		.ocon_num	= OCON_NUM - 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		.version	= POLICYDB_VERSION_IPV6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 		.sym_num	= SYM_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 		.version	= POLICYDB_VERSION_NLCLASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 		.sym_num	= SYM_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		.version	= POLICYDB_VERSION_MLS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		.version	= POLICYDB_VERSION_AVTAB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		.version	= POLICYDB_VERSION_RANGETRANS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		.version	= POLICYDB_VERSION_POLCAP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		.version	= POLICYDB_VERSION_PERMISSIVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		.version	= POLICYDB_VERSION_BOUNDARY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		.version	= POLICYDB_VERSION_FILENAME_TRANS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		.version	= POLICYDB_VERSION_ROLETRANS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		.version	= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		.version	= POLICYDB_VERSION_DEFAULT_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		.version	= POLICYDB_VERSION_CONSTRAINT_NAMES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		.version	= POLICYDB_VERSION_XPERMS_IOCTL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		.ocon_num	= OCON_NUM - 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		.version	= POLICYDB_VERSION_INFINIBAND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		.ocon_num	= OCON_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		.version	= POLICYDB_VERSION_GLBLUB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		.ocon_num	= OCON_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		.version	= POLICYDB_VERSION_COMP_FTRANS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		.sym_num	= SYM_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		.ocon_num	= OCON_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) static struct policydb_compat_info *policydb_lookup_compat(int version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	struct policydb_compat_info *info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	for (i = 0; i < ARRAY_SIZE(policydb_compat); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		if (policydb_compat[i].version == version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 			info = &policydb_compat[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	return info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179)  * The following *_destroy functions are used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180)  * free any memory allocated for each kind of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  * symbol data in the policy database.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) static int perm_destroy(void *key, void *datum, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) static int common_destroy(void *key, void *datum, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	struct common_datum *comdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	if (datum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		comdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		hashtab_map(&comdatum->permissions.table, perm_destroy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		hashtab_destroy(&comdatum->permissions.table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) static void constraint_expr_destroy(struct constraint_expr *expr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	if (expr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		ebitmap_destroy(&expr->names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		if (expr->type_names) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 			ebitmap_destroy(&expr->type_names->types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			ebitmap_destroy(&expr->type_names->negset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 			kfree(expr->type_names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		kfree(expr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) static int cls_destroy(void *key, void *datum, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	struct class_datum *cladatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	struct constraint_node *constraint, *ctemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	struct constraint_expr *e, *etmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	if (datum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		cladatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		hashtab_map(&cladatum->permissions.table, perm_destroy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		hashtab_destroy(&cladatum->permissions.table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		constraint = cladatum->constraints;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		while (constraint) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 			e = constraint->expr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 			while (e) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 				etmp = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 				e = e->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 				constraint_expr_destroy(etmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 			ctemp = constraint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 			constraint = constraint->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 			kfree(ctemp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		constraint = cladatum->validatetrans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		while (constraint) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 			e = constraint->expr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 			while (e) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 				etmp = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 				e = e->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 				constraint_expr_destroy(etmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 			ctemp = constraint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 			constraint = constraint->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 			kfree(ctemp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		kfree(cladatum->comkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) static int role_destroy(void *key, void *datum, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	struct role_datum *role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	if (datum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		role = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		ebitmap_destroy(&role->dominates);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		ebitmap_destroy(&role->types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) static int type_destroy(void *key, void *datum, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) static int user_destroy(void *key, void *datum, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	struct user_datum *usrdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	if (datum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		usrdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		ebitmap_destroy(&usrdatum->roles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		ebitmap_destroy(&usrdatum->range.level[0].cat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		ebitmap_destroy(&usrdatum->range.level[1].cat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		ebitmap_destroy(&usrdatum->dfltlevel.cat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) static int sens_destroy(void *key, void *datum, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	struct level_datum *levdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	if (datum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		levdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		if (levdatum->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			ebitmap_destroy(&levdatum->level->cat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		kfree(levdatum->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) static int cat_destroy(void *key, void *datum, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) static int (*destroy_f[SYM_NUM]) (void *key, void *datum, void *datap) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	common_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	cls_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	role_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	type_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	user_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	cond_destroy_bool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	sens_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	cat_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) static int filenametr_destroy(void *key, void *datum, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	struct filename_trans_key *ft = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	struct filename_trans_datum *next, *d = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	kfree(ft->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		ebitmap_destroy(&d->stypes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		next = d->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		kfree(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		d = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	} while (unlikely(d));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) static int range_tr_destroy(void *key, void *datum, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	struct mls_range *rt = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	ebitmap_destroy(&rt->level[0].cat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	ebitmap_destroy(&rt->level[1].cat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) static int role_tr_destroy(void *key, void *datum, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) static void ocontext_destroy(struct ocontext *c, int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	context_destroy(&c->context[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	context_destroy(&c->context[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	if (i == OCON_ISID || i == OCON_FS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	    i == OCON_NETIF || i == OCON_FSUSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		kfree(c->u.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	kfree(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381)  * Initialize the role table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) static int roles_init(struct policydb *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	char *key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	struct role_datum *role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	role = kzalloc(sizeof(*role), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	if (!role)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	role->value = ++p->p_roles.nprim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	if (role->value != OBJECT_R_VAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	key = kstrdup(OBJECT_R, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	if (!key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	rc = symtab_insert(&p->p_roles, key, role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	kfree(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	kfree(role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) static u32 filenametr_hash(const void *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	const struct filename_trans_key *ft = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	unsigned long hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	unsigned int byte_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	unsigned char focus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	hash = ft->ttype ^ ft->tclass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	byte_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	while ((focus = ft->name[byte_num++]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		hash = partial_name_hash(focus, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	return hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) static int filenametr_cmp(const void *k1, const void *k2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	const struct filename_trans_key *ft1 = k1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	const struct filename_trans_key *ft2 = k2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	int v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	v = ft1->ttype - ft2->ttype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	if (v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	v = ft1->tclass - ft2->tclass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	if (v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	return strcmp(ft1->name, ft2->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) static const struct hashtab_key_params filenametr_key_params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	.hash = filenametr_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	.cmp = filenametr_cmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) struct filename_trans_datum *policydb_filenametr_search(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	struct policydb *p, struct filename_trans_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	return hashtab_search(&p->filename_trans, key, filenametr_key_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) static u32 rangetr_hash(const void *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	const struct range_trans *key = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	return key->source_type + (key->target_type << 3) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		(key->target_class << 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) static int rangetr_cmp(const void *k1, const void *k2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	const struct range_trans *key1 = k1, *key2 = k2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	int v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	v = key1->source_type - key2->source_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	if (v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	v = key1->target_type - key2->target_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	if (v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	v = key1->target_class - key2->target_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) static const struct hashtab_key_params rangetr_key_params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	.hash = rangetr_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	.cmp = rangetr_cmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) struct mls_range *policydb_rangetr_search(struct policydb *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 					  struct range_trans *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	return hashtab_search(&p->range_tr, key, rangetr_key_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) static u32 role_trans_hash(const void *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	const struct role_trans_key *key = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	return key->role + (key->type << 3) + (key->tclass << 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) static int role_trans_cmp(const void *k1, const void *k2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	const struct role_trans_key *key1 = k1, *key2 = k2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	int v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	v = key1->role - key2->role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	if (v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	v = key1->type - key2->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	if (v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	return key1->tclass - key2->tclass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) static const struct hashtab_key_params roletr_key_params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	.hash = role_trans_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	.cmp = role_trans_cmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) struct role_trans_datum *policydb_roletr_search(struct policydb *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 						struct role_trans_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	return hashtab_search(&p->role_tr, key, roletr_key_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530)  * Initialize a policy database structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) static void policydb_init(struct policydb *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	memset(p, 0, sizeof(*p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	avtab_init(&p->te_avtab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	cond_policydb_init(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	ebitmap_init(&p->filename_trans_ttypes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	ebitmap_init(&p->policycaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	ebitmap_init(&p->permissive_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545)  * The following *_index functions are used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546)  * define the val_to_name and val_to_struct arrays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547)  * in a policy database structure.  The val_to_name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548)  * arrays are used when converting security context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549)  * structures into string representations.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550)  * val_to_struct arrays are used when the attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551)  * of a class, role, or user are needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) static int common_index(void *key, void *datum, void *datap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	struct policydb *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	struct common_datum *comdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	comdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	p = datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	if (!comdatum->value || comdatum->value > p->p_commons.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	p->sym_val_to_name[SYM_COMMONS][comdatum->value - 1] = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) static int class_index(void *key, void *datum, void *datap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	struct policydb *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	struct class_datum *cladatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	cladatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	p = datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	if (!cladatum->value || cladatum->value > p->p_classes.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	p->sym_val_to_name[SYM_CLASSES][cladatum->value - 1] = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	p->class_val_to_struct[cladatum->value - 1] = cladatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) static int role_index(void *key, void *datum, void *datap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	struct policydb *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	struct role_datum *role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	role = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	p = datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	if (!role->value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	    || role->value > p->p_roles.nprim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	    || role->bounds > p->p_roles.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	p->sym_val_to_name[SYM_ROLES][role->value - 1] = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	p->role_val_to_struct[role->value - 1] = role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) static int type_index(void *key, void *datum, void *datap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	struct policydb *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	struct type_datum *typdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	typdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	p = datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	if (typdatum->primary) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		if (!typdatum->value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		    || typdatum->value > p->p_types.nprim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		    || typdatum->bounds > p->p_types.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		p->sym_val_to_name[SYM_TYPES][typdatum->value - 1] = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		p->type_val_to_struct[typdatum->value - 1] = typdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) static int user_index(void *key, void *datum, void *datap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	struct policydb *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	struct user_datum *usrdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	usrdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	p = datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	if (!usrdatum->value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	    || usrdatum->value > p->p_users.nprim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	    || usrdatum->bounds > p->p_users.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	p->sym_val_to_name[SYM_USERS][usrdatum->value - 1] = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	p->user_val_to_struct[usrdatum->value - 1] = usrdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) static int sens_index(void *key, void *datum, void *datap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	struct policydb *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	struct level_datum *levdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	levdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	p = datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	if (!levdatum->isalias) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		if (!levdatum->level->sens ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		    levdatum->level->sens > p->p_levels.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		p->sym_val_to_name[SYM_LEVELS][levdatum->level->sens - 1] = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) static int cat_index(void *key, void *datum, void *datap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	struct policydb *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	struct cat_datum *catdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	catdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	p = datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	if (!catdatum->isalias) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		if (!catdatum->value || catdatum->value > p->p_cats.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		p->sym_val_to_name[SYM_CATS][catdatum->value - 1] = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) static int (*index_f[SYM_NUM]) (void *key, void *datum, void *datap) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	common_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	class_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	role_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	type_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	user_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	cond_index_bool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	sens_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	cat_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) #ifdef DEBUG_HASHES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) static void hash_eval(struct hashtab *h, const char *hash_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	struct hashtab_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	hashtab_stat(h, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	pr_debug("SELinux: %s:  %d entries and %d/%d buckets used, longest chain length %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		 hash_name, h->nel, info.slots_used, h->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		 info.max_chain_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) static void symtab_hash_eval(struct symtab *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	for (i = 0; i < SYM_NUM; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		hash_eval(&s[i].table, symtab_name[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) static inline void hash_eval(struct hashtab *h, char *hash_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713)  * Define the other val_to_name and val_to_struct arrays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714)  * in a policy database structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716)  * Caller must clean up on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) static int policydb_index(struct policydb *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	if (p->mls_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		pr_debug("SELinux:  %d users, %d roles, %d types, %d bools, %d sens, %d cats\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 			 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			 p->p_bools.nprim, p->p_levels.nprim, p->p_cats.nprim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		pr_debug("SELinux:  %d users, %d roles, %d types, %d bools\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 			 p->p_bools.nprim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	pr_debug("SELinux:  %d classes, %d rules\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		 p->p_classes.nprim, p->te_avtab.nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) #ifdef DEBUG_HASHES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	avtab_hash_eval(&p->te_avtab, "rules");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	symtab_hash_eval(p->symtab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	p->class_val_to_struct = kcalloc(p->p_classes.nprim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 					 sizeof(*p->class_val_to_struct),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 					 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	if (!p->class_val_to_struct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	p->role_val_to_struct = kcalloc(p->p_roles.nprim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 					sizeof(*p->role_val_to_struct),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	if (!p->role_val_to_struct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	p->user_val_to_struct = kcalloc(p->p_users.nprim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 					sizeof(*p->user_val_to_struct),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	if (!p->user_val_to_struct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	p->type_val_to_struct = kvcalloc(p->p_types.nprim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 					 sizeof(*p->type_val_to_struct),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 					 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	if (!p->type_val_to_struct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	rc = cond_init_bool_indexes(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	for (i = 0; i < SYM_NUM; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		p->sym_val_to_name[i] = kvcalloc(p->symtab[i].nprim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 						 sizeof(char *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 						 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		if (!p->sym_val_to_name[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		rc = hashtab_map(&p->symtab[i].table, index_f[i], p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784)  * Free any memory allocated by a policy database structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) void policydb_destroy(struct policydb *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	struct ocontext *c, *ctmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	struct genfs *g, *gtmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	struct role_allow *ra, *lra = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	for (i = 0; i < SYM_NUM; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		hashtab_map(&p->symtab[i].table, destroy_f[i], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		hashtab_destroy(&p->symtab[i].table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	for (i = 0; i < SYM_NUM; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		kvfree(p->sym_val_to_name[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	kfree(p->class_val_to_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	kfree(p->role_val_to_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	kfree(p->user_val_to_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	kvfree(p->type_val_to_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	avtab_destroy(&p->te_avtab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	for (i = 0; i < OCON_NUM; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		c = p->ocontexts[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		while (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 			ctmp = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 			c = c->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 			ocontext_destroy(ctmp, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		p->ocontexts[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	g = p->genfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	while (g) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		kfree(g->fstype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		c = g->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		while (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			ctmp = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			c = c->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 			ocontext_destroy(ctmp, OCON_FSUSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		gtmp = g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		g = g->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		kfree(gtmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	p->genfs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	cond_policydb_destroy(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	hashtab_map(&p->role_tr, role_tr_destroy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	hashtab_destroy(&p->role_tr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	for (ra = p->role_allow; ra; ra = ra->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		kfree(lra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		lra = ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	kfree(lra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	hashtab_map(&p->filename_trans, filenametr_destroy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	hashtab_destroy(&p->filename_trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	hashtab_map(&p->range_tr, range_tr_destroy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	hashtab_destroy(&p->range_tr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	if (p->type_attr_map_array) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		for (i = 0; i < p->p_types.nprim; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 			ebitmap_destroy(&p->type_attr_map_array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		kvfree(p->type_attr_map_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	ebitmap_destroy(&p->filename_trans_ttypes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	ebitmap_destroy(&p->policycaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	ebitmap_destroy(&p->permissive_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866)  * Load the initial SIDs specified in a policy database
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867)  * structure into a SID table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) int policydb_load_isids(struct policydb *p, struct sidtab *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	struct ocontext *head, *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	rc = sidtab_init(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		pr_err("SELinux:  out of memory on SID table init\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	head = p->ocontexts[OCON_ISID];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	for (c = head; c; c = c->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		u32 sid = c->sid[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		const char *name = security_get_initial_sid_context(sid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		if (sid == SECSID_NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			pr_err("SELinux:  SID 0 was assigned a context.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 			sidtab_destroy(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		/* Ignore initial SIDs unused by this kernel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		rc = sidtab_set_initial(s, sid, &c->context[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 			pr_err("SELinux:  unable to load initial SID %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 			       name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 			sidtab_destroy(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) int policydb_class_isvalid(struct policydb *p, unsigned int class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	if (!class || class > p->p_classes.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) int policydb_role_isvalid(struct policydb *p, unsigned int role)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	if (!role || role > p->p_roles.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) int policydb_type_isvalid(struct policydb *p, unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	if (!type || type > p->p_types.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928)  * Return 1 if the fields in the security context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929)  * structure `c' are valid.  Return 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) int policydb_context_isvalid(struct policydb *p, struct context *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	struct role_datum *role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	struct user_datum *usrdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	if (!c->role || c->role > p->p_roles.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (!c->user || c->user > p->p_users.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	if (!c->type || c->type > p->p_types.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	if (c->role != OBJECT_R_VAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		 * Role must be authorized for the type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		role = p->role_val_to_struct[c->role - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		if (!role || !ebitmap_get_bit(&role->types, c->type - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			/* role may not be associated with type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		 * User must be authorized for the role.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		usrdatum = p->user_val_to_struct[c->user - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		if (!usrdatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		if (!ebitmap_get_bit(&usrdatum->roles, c->role - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			/* user may not be associated with role */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	if (!mls_context_isvalid(p, c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973)  * Read a MLS range structure from a policydb binary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974)  * representation file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) static int mls_read_range_helper(struct mls_range *r, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	__le32 buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	u32 items;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	items = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	if (items > ARRAY_SIZE(buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		pr_err("SELinux: mls:  range overflow\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	rc = next_entry(buf, fp, sizeof(u32) * items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		pr_err("SELinux: mls:  truncated range\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	r->level[0].sens = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	if (items > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		r->level[1].sens = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		r->level[1].sens = r->level[0].sens;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	rc = ebitmap_read(&r->level[0].cat, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		pr_err("SELinux: mls:  error reading low categories\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	if (items > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		rc = ebitmap_read(&r->level[1].cat, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			pr_err("SELinux: mls:  error reading high categories\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 			goto bad_high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		rc = ebitmap_cpy(&r->level[1].cat, &r->level[0].cat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 			pr_err("SELinux: mls:  out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 			goto bad_high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) bad_high:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	ebitmap_destroy(&r->level[0].cat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)  * Read and validate a security context structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)  * from a policydb binary representation file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) static int context_read_and_validate(struct context *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 				     struct policydb *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 				     void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	rc = next_entry(buf, fp, sizeof buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		pr_err("SELinux: context truncated\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	c->user = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	c->role = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	c->type = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	if (p->policyvers >= POLICYDB_VERSION_MLS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		rc = mls_read_range_helper(&c->range, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			pr_err("SELinux: error reading MLS range of context\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	if (!policydb_context_isvalid(p, c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		pr_err("SELinux:  invalid security context\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		context_destroy(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)  * The following *_read functions are used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)  * read the symbol data from a policy database
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)  * binary representation file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	if ((len == 0) || (len == (u32)-1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	str = kmalloc(len + 1, flags | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	if (!str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	rc = next_entry(str, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		kfree(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	str[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	*strp = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) static int perm_read(struct policydb *p, struct symtab *s, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	char *key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	struct perm_datum *perdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	__le32 buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	perdatum = kzalloc(sizeof(*perdatum), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	if (!perdatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	rc = next_entry(buf, fp, sizeof buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	perdatum->value = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	rc = str_read(&key, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	rc = symtab_insert(s, key, perdatum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	perm_destroy(key, perdatum, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) static int common_read(struct policydb *p, struct symtab *s, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	char *key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	struct common_datum *comdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	__le32 buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	u32 len, nel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	comdatum = kzalloc(sizeof(*comdatum), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	if (!comdatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	rc = next_entry(buf, fp, sizeof buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	comdatum->value = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	nel = le32_to_cpu(buf[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	rc = symtab_init(&comdatum->permissions, nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	comdatum->permissions.nprim = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	rc = str_read(&key, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	for (i = 0; i < nel; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		rc = perm_read(p, &comdatum->permissions, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	rc = symtab_insert(s, key, comdatum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	common_destroy(key, comdatum, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) static void type_set_init(struct type_set *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	ebitmap_init(&t->types);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	ebitmap_init(&t->negset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) static int type_set_read(struct type_set *t, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	__le32 buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	if (ebitmap_read(&t->types, fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	if (ebitmap_read(&t->negset, fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	t->flags = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) static int read_cons_helper(struct policydb *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 				struct constraint_node **nodep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 				int ncons, int allowxtarget, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	struct constraint_node *c, *lc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	struct constraint_expr *e, *le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	u32 nexpr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	int rc, i, j, depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	lc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	for (i = 0; i < ncons; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		c = kzalloc(sizeof(*c), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		if (lc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 			lc->next = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 			*nodep = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		rc = next_entry(buf, fp, (sizeof(u32) * 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		c->permissions = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		nexpr = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		le = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		depth = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		for (j = 0; j < nexpr; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 			e = kzalloc(sizeof(*e), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 			if (!e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			if (le)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 				le->next = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 				c->expr = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 			rc = next_entry(buf, fp, (sizeof(u32) * 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 			e->expr_type = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 			e->attr = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			e->op = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 			switch (e->expr_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 			case CEXPR_NOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 				if (depth < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 					return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 			case CEXPR_AND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 			case CEXPR_OR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 				if (depth < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 					return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 				depth--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 			case CEXPR_ATTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 				if (depth == (CEXPR_MAXDEPTH - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 					return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 				depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 			case CEXPR_NAMES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 				if (!allowxtarget && (e->attr & CEXPR_XTARGET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 					return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 				if (depth == (CEXPR_MAXDEPTH - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 					return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 				depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 				rc = ebitmap_read(&e->names, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 				if (p->policyvers >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 				    POLICYDB_VERSION_CONSTRAINT_NAMES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 					e->type_names = kzalloc(sizeof
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 						(*e->type_names), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 					if (!e->type_names)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 						return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 					type_set_init(e->type_names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 					rc = type_set_read(e->type_names, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 					if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 						return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 			le = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		if (depth != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		lc = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) static int class_read(struct policydb *p, struct symtab *s, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	char *key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	struct class_datum *cladatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	__le32 buf[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	u32 len, len2, ncons, nel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	cladatum = kzalloc(sizeof(*cladatum), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	if (!cladatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	rc = next_entry(buf, fp, sizeof(u32)*6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	len2 = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	cladatum->value = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	nel = le32_to_cpu(buf[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	rc = symtab_init(&cladatum->permissions, nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	cladatum->permissions.nprim = le32_to_cpu(buf[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	ncons = le32_to_cpu(buf[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	rc = str_read(&key, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	if (len2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		rc = str_read(&cladatum->comkey, GFP_KERNEL, fp, len2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 		cladatum->comdatum = symtab_search(&p->p_commons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 						   cladatum->comkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		if (!cladatum->comdatum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			pr_err("SELinux:  unknown common %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 			       cladatum->comkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	for (i = 0; i < nel; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		rc = perm_read(p, &cladatum->permissions, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	if (p->policyvers >= POLICYDB_VERSION_VALIDATETRANS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		/* grab the validatetrans rules */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		ncons = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		rc = read_cons_helper(p, &cladatum->validatetrans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 				ncons, 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		rc = next_entry(buf, fp, sizeof(u32) * 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		cladatum->default_user = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		cladatum->default_role = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		cladatum->default_range = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		rc = next_entry(buf, fp, sizeof(u32) * 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		cladatum->default_type = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	rc = symtab_insert(s, key, cladatum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	cls_destroy(key, cladatum, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) static int role_read(struct policydb *p, struct symtab *s, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	char *key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	struct role_datum *role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	int rc, to_read = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	role = kzalloc(sizeof(*role), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	if (!role)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		to_read = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	role->value = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		role->bounds = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	rc = str_read(&key, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	rc = ebitmap_read(&role->dominates, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	rc = ebitmap_read(&role->types, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	if (strcmp(key, OBJECT_R) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		if (role->value != OBJECT_R_VAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 			pr_err("SELinux: Role %s has wrong value %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 			       OBJECT_R, role->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	rc = symtab_insert(s, key, role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	role_destroy(key, role, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) static int type_read(struct policydb *p, struct symtab *s, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	char *key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	struct type_datum *typdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	int rc, to_read = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	__le32 buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	typdatum = kzalloc(sizeof(*typdatum), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	if (!typdatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		to_read = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	typdatum->value = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		u32 prop = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		if (prop & TYPEDATUM_PROPERTY_PRIMARY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 			typdatum->primary = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		if (prop & TYPEDATUM_PROPERTY_ATTRIBUTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 			typdatum->attribute = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		typdatum->bounds = le32_to_cpu(buf[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		typdatum->primary = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	rc = str_read(&key, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	rc = symtab_insert(s, key, typdatum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	type_destroy(key, typdatum, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)  * Read a MLS level structure from a policydb binary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)  * representation file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) static int mls_read_level(struct mls_level *lp, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	__le32 buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	memset(lp, 0, sizeof(*lp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	rc = next_entry(buf, fp, sizeof buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		pr_err("SELinux: mls: truncated level\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	lp->sens = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	rc = ebitmap_read(&lp->cat, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		pr_err("SELinux: mls:  error reading level categories\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) static int user_read(struct policydb *p, struct symtab *s, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	char *key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	struct user_datum *usrdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	int rc, to_read = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	usrdatum = kzalloc(sizeof(*usrdatum), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	if (!usrdatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		to_read = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	rc = next_entry(buf, fp, sizeof(buf[0]) * to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	usrdatum->value = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		usrdatum->bounds = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	rc = str_read(&key, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	rc = ebitmap_read(&usrdatum->roles, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	if (p->policyvers >= POLICYDB_VERSION_MLS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		rc = mls_read_range_helper(&usrdatum->range, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		rc = mls_read_level(&usrdatum->dfltlevel, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	rc = symtab_insert(s, key, usrdatum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	user_destroy(key, usrdatum, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) static int sens_read(struct policydb *p, struct symtab *s, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	char *key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	struct level_datum *levdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	__le32 buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	levdatum = kzalloc(sizeof(*levdatum), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	if (!levdatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	rc = next_entry(buf, fp, sizeof buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	levdatum->isalias = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	rc = str_read(&key, GFP_ATOMIC, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	levdatum->level = kmalloc(sizeof(*levdatum->level), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	if (!levdatum->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	rc = mls_read_level(levdatum->level, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	rc = symtab_insert(s, key, levdatum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	sens_destroy(key, levdatum, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) static int cat_read(struct policydb *p, struct symtab *s, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	char *key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	struct cat_datum *catdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	catdatum = kzalloc(sizeof(*catdatum), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	if (!catdatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	rc = next_entry(buf, fp, sizeof buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	catdatum->value = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	catdatum->isalias = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	rc = str_read(&key, GFP_ATOMIC, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	rc = symtab_insert(s, key, catdatum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	cat_destroy(key, catdatum, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) static int (*read_f[SYM_NUM]) (struct policydb *p, struct symtab *s, void *fp) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	common_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	class_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	role_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	type_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	user_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	cond_read_bool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	sens_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	cat_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) static int user_bounds_sanity_check(void *key, void *datum, void *datap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	struct user_datum *upper, *user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	struct policydb *p = datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	int depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	upper = user = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	while (upper->bounds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		struct ebitmap_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		unsigned long bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 			pr_err("SELinux: user %s: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 			       "too deep or looped boundary",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 			       (char *) key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		upper = p->user_val_to_struct[upper->bounds - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		ebitmap_for_each_positive_bit(&user->roles, node, bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 			if (ebitmap_get_bit(&upper->roles, bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 			pr_err("SELinux: boundary violated policy: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 			       "user=%s role=%s bounds=%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 			       sym_name(p, SYM_USERS, user->value - 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 			       sym_name(p, SYM_ROLES, bit),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 			       sym_name(p, SYM_USERS, upper->value - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) static int role_bounds_sanity_check(void *key, void *datum, void *datap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	struct role_datum *upper, *role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	struct policydb *p = datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	int depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	upper = role = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	while (upper->bounds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		struct ebitmap_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 		unsigned long bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 			pr_err("SELinux: role %s: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 			       "too deep or looped bounds\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 			       (char *) key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		upper = p->role_val_to_struct[upper->bounds - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		ebitmap_for_each_positive_bit(&role->types, node, bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 			if (ebitmap_get_bit(&upper->types, bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 			pr_err("SELinux: boundary violated policy: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 			       "role=%s type=%s bounds=%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 			       sym_name(p, SYM_ROLES, role->value - 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 			       sym_name(p, SYM_TYPES, bit),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 			       sym_name(p, SYM_ROLES, upper->value - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) static int type_bounds_sanity_check(void *key, void *datum, void *datap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	struct type_datum *upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	struct policydb *p = datap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	int depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	upper = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	while (upper->bounds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		if (++depth == POLICYDB_BOUNDS_MAXDEPTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 			pr_err("SELinux: type %s: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			       "too deep or looped boundary\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 			       (char *) key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		upper = p->type_val_to_struct[upper->bounds - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		BUG_ON(!upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		if (upper->attribute) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 			pr_err("SELinux: type %s: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 			       "bounded by attribute %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 			       (char *) key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 			       sym_name(p, SYM_TYPES, upper->value - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) static int policydb_bounds_sanity_check(struct policydb *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	if (p->policyvers < POLICYDB_VERSION_BOUNDARY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	rc = hashtab_map(&p->p_users.table, user_bounds_sanity_check, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	rc = hashtab_map(&p->p_roles.table, role_bounds_sanity_check, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	rc = hashtab_map(&p->p_types.table, type_bounds_sanity_check, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) u16 string_to_security_class(struct policydb *p, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	struct class_datum *cladatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	cladatum = symtab_search(&p->p_classes, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	if (!cladatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	return cladatum->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	struct class_datum *cladatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	struct perm_datum *perdatum = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	struct common_datum *comdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	if (!tclass || tclass > p->p_classes.nprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	cladatum = p->class_val_to_struct[tclass-1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	comdatum = cladatum->comdatum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	if (comdatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		perdatum = symtab_search(&comdatum->permissions, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	if (!perdatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		perdatum = symtab_search(&cladatum->permissions, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	if (!perdatum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	return 1U << (perdatum->value-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) static int range_read(struct policydb *p, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	struct range_trans *rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	struct mls_range *r = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	__le32 buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	u32 nel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	if (p->policyvers < POLICYDB_VERSION_MLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	nel = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	rc = hashtab_init(&p->range_tr, nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	for (i = 0; i < nel; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 		rt = kzalloc(sizeof(*rt), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		rc = next_entry(buf, fp, (sizeof(u32) * 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		rt->source_type = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 		rt->target_type = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 			rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 			rt->target_class = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 			rt->target_class = p->process_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		if (!policydb_type_isvalid(p, rt->source_type) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		    !policydb_type_isvalid(p, rt->target_type) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		    !policydb_class_isvalid(p, rt->target_class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 		r = kzalloc(sizeof(*r), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 		if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 		rc = mls_read_range_helper(r, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 		if (!mls_range_isvalid(p, r)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 			pr_warn("SELinux:  rangetrans:  invalid range\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 		rc = hashtab_insert(&p->range_tr, rt, r, rangetr_key_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 		rt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 		r = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	hash_eval(&p->range_tr, "rangetr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	kfree(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	kfree(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) static int filename_trans_read_helper_compat(struct policydb *p, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	struct filename_trans_key key, *ft = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	struct filename_trans_datum *last, *datum = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	u32 len, stype, otype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	__le32 buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	/* length of the path component string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	/* path component string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	rc = str_read(&name, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	rc = next_entry(buf, fp, sizeof(u32) * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	stype = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	key.ttype = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	key.tclass = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	key.name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	otype = le32_to_cpu(buf[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	datum = policydb_filenametr_search(p, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	while (datum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 		if (unlikely(ebitmap_get_bit(&datum->stypes, stype - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 			/* conflicting/duplicate rules are ignored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 			datum = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 		if (likely(datum->otype == otype))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		last = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 		datum = datum->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	if (!datum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 		datum = kmalloc(sizeof(*datum), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 		if (!datum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 		ebitmap_init(&datum->stypes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		datum->otype = otype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		datum->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 		if (unlikely(last)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 			last->next = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 			ft = kmemdup(&key, sizeof(key), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 			if (!ft)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 			rc = hashtab_insert(&p->filename_trans, ft, datum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 					    filenametr_key_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 			name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 			rc = ebitmap_set_bit(&p->filename_trans_ttypes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 					     key.ttype, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	return ebitmap_set_bit(&datum->stypes, stype - 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	kfree(ft);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) static int filename_trans_read_helper(struct policydb *p, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	struct filename_trans_key *ft = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	struct filename_trans_datum **dst, *datum, *first = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	u32 len, ttype, tclass, ndatum, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	/* length of the path component string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	/* path component string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	rc = str_read(&name, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	rc = next_entry(buf, fp, sizeof(u32) * 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	ttype = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	tclass = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	ndatum = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	if (ndatum == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		pr_err("SELinux:  Filename transition key with no datum\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 		rc = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	dst = &first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	for (i = 0; i < ndatum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		datum = kmalloc(sizeof(*datum), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 		if (!datum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		*dst = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		/* ebitmap_read() will at least init the bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 		rc = ebitmap_read(&datum->stypes, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 		datum->otype = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 		datum->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 		dst = &datum->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	ft = kmalloc(sizeof(*ft), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	if (!ft)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	ft->ttype = ttype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	ft->tclass = tclass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	ft->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	rc = hashtab_insert(&p->filename_trans, ft, first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 			    filenametr_key_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	if (rc == -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		pr_err("SELinux:  Duplicate filename transition key\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 	return ebitmap_set_bit(&p->filename_trans_ttypes, ttype, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	kfree(ft);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	while (first) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 		datum = first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 		first = first->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 		ebitmap_destroy(&datum->stypes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 		kfree(datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) static int filename_trans_read(struct policydb *p, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	u32 nel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	__le32 buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	int rc, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	nel = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 		p->compat_filename_trans_count = nel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 		rc = hashtab_init(&p->filename_trans, (1 << 11));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 		for (i = 0; i < nel; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 			rc = filename_trans_read_helper_compat(p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 		rc = hashtab_init(&p->filename_trans, nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 		for (i = 0; i < nel; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 			rc = filename_trans_read_helper(p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	hash_eval(&p->filename_trans, "filenametr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) static int genfs_read(struct policydb *p, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	int i, j, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	u32 nel, nel2, len, len2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	__le32 buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	struct ocontext *l, *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	struct ocontext *newc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	struct genfs *genfs_p, *genfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	struct genfs *newgenfs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	nel = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	for (i = 0; i < nel; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 		rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 		len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		newgenfs = kzalloc(sizeof(*newgenfs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 		if (!newgenfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 		rc = str_read(&newgenfs->fstype, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		for (genfs_p = NULL, genfs = p->genfs; genfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		     genfs_p = genfs, genfs = genfs->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 			rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 			if (strcmp(newgenfs->fstype, genfs->fstype) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 				pr_err("SELinux:  dup genfs fstype %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 				       newgenfs->fstype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 			if (strcmp(newgenfs->fstype, genfs->fstype) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 		newgenfs->next = genfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 		if (genfs_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 			genfs_p->next = newgenfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 			p->genfs = newgenfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 		genfs = newgenfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 		newgenfs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 		rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 		nel2 = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 		for (j = 0; j < nel2; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 			rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 			len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 			newc = kzalloc(sizeof(*newc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 			if (!newc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 			rc = str_read(&newc->u.name, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 			rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 			newc->v.sclass = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 			rc = context_read_and_validate(&newc->context[0], p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 			for (l = NULL, c = genfs->head; c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 			     l = c, c = c->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 				rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 				if (!strcmp(newc->u.name, c->u.name) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 				    (!c->v.sclass || !newc->v.sclass ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 				     newc->v.sclass == c->v.sclass)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 					pr_err("SELinux:  dup genfs entry (%s,%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 					       genfs->fstype, c->u.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 				len = strlen(newc->u.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 				len2 = strlen(c->u.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 				if (len > len2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 			newc->next = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 			if (l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 				l->next = newc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 				genfs->head = newc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 			newc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	if (newgenfs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 		kfree(newgenfs->fstype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 		kfree(newgenfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	ocontext_destroy(newc, OCON_FSUSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 			 void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	int i, j, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	u32 nel, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	__be64 prefixbuf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	struct ocontext *l, *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	u32 nodebuf[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	for (i = 0; i < info->ocon_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 		rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 		nel = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 		l = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 		for (j = 0; j < nel; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 			c = kzalloc(sizeof(*c), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 			if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 			if (l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 				l->next = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 				p->ocontexts[i] = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 			l = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 			switch (i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 			case OCON_ISID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 				rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 				c->sid[0] = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 				rc = context_read_and_validate(&c->context[0], p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 			case OCON_FS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 			case OCON_NETIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 				rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 				len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 				rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 				rc = context_read_and_validate(&c->context[0], p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 				rc = context_read_and_validate(&c->context[1], p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 			case OCON_PORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 				rc = next_entry(buf, fp, sizeof(u32)*3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 				c->u.port.protocol = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 				c->u.port.low_port = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 				c->u.port.high_port = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 				rc = context_read_and_validate(&c->context[0], p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 			case OCON_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 				rc = next_entry(nodebuf, fp, sizeof(u32) * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 				c->u.node.addr = nodebuf[0]; /* network order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 				c->u.node.mask = nodebuf[1]; /* network order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 				rc = context_read_and_validate(&c->context[0], p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 			case OCON_FSUSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 				rc = next_entry(buf, fp, sizeof(u32)*2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 				rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 				c->v.behavior = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 				/* Determined at runtime, not in policy DB. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 				if (c->v.behavior == SECURITY_FS_USE_MNTPOINT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 				if (c->v.behavior > SECURITY_FS_USE_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 				len = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 				rc = str_read(&c->u.name, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 				rc = context_read_and_validate(&c->context[0], p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 			case OCON_NODE6: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 				int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 				rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 				for (k = 0; k < 4; k++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 					c->u.node6.addr[k] = nodebuf[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 				for (k = 0; k < 4; k++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 					c->u.node6.mask[k] = nodebuf[k+4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 				rc = context_read_and_validate(&c->context[0], p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 			case OCON_IBPKEY: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 				u32 pkey_lo, pkey_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 				rc = next_entry(prefixbuf, fp, sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 				/* we need to have subnet_prefix in CPU order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 				c->u.ibpkey.subnet_prefix = be64_to_cpu(prefixbuf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 				rc = next_entry(buf, fp, sizeof(u32) * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 				pkey_lo = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 				pkey_hi = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 				if (pkey_lo > U16_MAX || pkey_hi > U16_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 					rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 				c->u.ibpkey.low_pkey  = pkey_lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 				c->u.ibpkey.high_pkey = pkey_hi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 				rc = context_read_and_validate(&c->context[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 							       p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 							       fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 			case OCON_IBENDPORT: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 				u32 port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 				rc = next_entry(buf, fp, sizeof(u32) * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 				len = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 				rc = str_read(&c->u.ibendport.dev_name, GFP_KERNEL, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 				port = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 				if (port > U8_MAX || port == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 					rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 				c->u.ibendport.port = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 				rc = context_read_and_validate(&c->context[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 							       p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 							       fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 			} /* end case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 			} /* end switch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399)  * Read the configuration data from a policy database binary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400)  * representation file into a policy database structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) int policydb_read(struct policydb *p, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	struct role_allow *ra, *lra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	struct role_trans_key *rtk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	struct role_trans_datum *rtd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	int i, j, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	__le32 buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	u32 len, nprim, nel, perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	char *policydb_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	struct policydb_compat_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	policydb_init(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 	/* Read the magic number and string length. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 	rc = next_entry(buf, fp, sizeof(u32) * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 	if (le32_to_cpu(buf[0]) != POLICYDB_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 		pr_err("SELinux:  policydb magic number 0x%x does "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 		       "not match expected magic number 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 		       le32_to_cpu(buf[0]), POLICYDB_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	len = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 	if (len != strlen(POLICYDB_STRING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 		pr_err("SELinux:  policydb string length %d does not "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 		       "match expected length %zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 		       len, strlen(POLICYDB_STRING));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 	rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 	policydb_str = kmalloc(len + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 	if (!policydb_str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 		pr_err("SELinux:  unable to allocate memory for policydb "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 		       "string of length %d\n", len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	rc = next_entry(policydb_str, fp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 		pr_err("SELinux:  truncated policydb string identifier\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 		kfree(policydb_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 	policydb_str[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 	if (strcmp(policydb_str, POLICYDB_STRING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 		pr_err("SELinux:  policydb string %s does not match "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 		       "my string %s\n", policydb_str, POLICYDB_STRING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 		kfree(policydb_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 	/* Done with policydb_str. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 	kfree(policydb_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	policydb_str = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	/* Read the version and table sizes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 	rc = next_entry(buf, fp, sizeof(u32)*4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	p->policyvers = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	if (p->policyvers < POLICYDB_VERSION_MIN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	    p->policyvers > POLICYDB_VERSION_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 		pr_err("SELinux:  policydb version %d does not match "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 		       "my version range %d-%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 		       le32_to_cpu(buf[0]), POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_MLS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 		p->mls_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 		if (p->policyvers < POLICYDB_VERSION_MLS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 			pr_err("SELinux: security policydb version %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 				"(MLS) not backwards compatible\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 				p->policyvers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	p->reject_unknown = !!(le32_to_cpu(buf[1]) & REJECT_UNKNOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	p->allow_unknown = !!(le32_to_cpu(buf[1]) & ALLOW_UNKNOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_ANDROID_NETLINK_ROUTE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 		p->android_netlink_route = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 	if ((le32_to_cpu(buf[1]) & POLICYDB_CONFIG_ANDROID_NETLINK_GETNEIGH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 		p->android_netlink_getneigh = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 	if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 		rc = ebitmap_read(&p->policycaps, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 		rc = ebitmap_read(&p->permissive_map, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	info = policydb_lookup_compat(p->policyvers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	if (!info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 		pr_err("SELinux:  unable to find policy compat info "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 		       "for version %d\n", p->policyvers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	if (le32_to_cpu(buf[2]) != info->sym_num ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 		le32_to_cpu(buf[3]) != info->ocon_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 		pr_err("SELinux:  policydb table sizes (%d,%d) do "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 		       "not match mine (%d,%d)\n", le32_to_cpu(buf[2]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 			le32_to_cpu(buf[3]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 		       info->sym_num, info->ocon_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	for (i = 0; i < info->sym_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 		rc = next_entry(buf, fp, sizeof(u32)*2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 		nprim = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 		nel = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 		rc = symtab_init(&p->symtab[i], nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 		if (i == SYM_ROLES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 			rc = roles_init(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 		for (j = 0; j < nel; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 			rc = read_f[i](p, &p->symtab[i], fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 		p->symtab[i].nprim = nprim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 	p->process_class = string_to_security_class(p, "process");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 	if (!p->process_class) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 		pr_err("SELinux: process class is required, not defined in policy\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 	rc = avtab_read(&p->te_avtab, fp, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 	if (p->policyvers >= POLICYDB_VERSION_BOOL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 		rc = cond_read_list(p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 	nel = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 	rc = hashtab_init(&p->role_tr, nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	for (i = 0; i < nel; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 		rtk = kmalloc(sizeof(*rtk), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 		if (!rtk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 		rtd = kmalloc(sizeof(*rtd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 		if (!rtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 		rc = next_entry(buf, fp, sizeof(u32)*3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 		rtk->role = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 		rtk->type = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 		rtd->new_role = le32_to_cpu(buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 		if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 			rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 			rtk->tclass = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 			rtk->tclass = p->process_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 		if (!policydb_role_isvalid(p, rtk->role) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 		    !policydb_type_isvalid(p, rtk->type) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 		    !policydb_class_isvalid(p, rtk->tclass) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 		    !policydb_role_isvalid(p, rtd->new_role))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 		rc = hashtab_insert(&p->role_tr, rtk, rtd, roletr_key_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 		rtk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 		rtd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	rc = next_entry(buf, fp, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	nel = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 	lra = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	for (i = 0; i < nel; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 		ra = kzalloc(sizeof(*ra), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 		if (!ra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 		if (lra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 			lra->next = ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 			p->role_allow = ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 		rc = next_entry(buf, fp, sizeof(u32)*2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 		ra->role = le32_to_cpu(buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 		ra->new_role = le32_to_cpu(buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 		if (!policydb_role_isvalid(p, ra->role) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 		    !policydb_role_isvalid(p, ra->new_role))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 		lra = ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 	rc = filename_trans_read(p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 	rc = policydb_index(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 	perm = string_to_av_perm(p, p->process_class, "transition");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	if (!perm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 		pr_err("SELinux: process transition permission is required, not defined in policy\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 	p->process_trans_perms = perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 	perm = string_to_av_perm(p, p->process_class, "dyntransition");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 	if (!perm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 		pr_err("SELinux: process dyntransition permission is required, not defined in policy\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 	p->process_trans_perms |= perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 	rc = ocontext_read(p, info, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 	rc = genfs_read(p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 	rc = range_read(p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	p->type_attr_map_array = kvcalloc(p->p_types.nprim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 					  sizeof(*p->type_attr_map_array),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 					  GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	if (!p->type_attr_map_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	/* just in case ebitmap_init() becomes more than just a memset(0): */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	for (i = 0; i < p->p_types.nprim; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 		ebitmap_init(&p->type_attr_map_array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	for (i = 0; i < p->p_types.nprim; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 		struct ebitmap *e = &p->type_attr_map_array[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 		if (p->policyvers >= POLICYDB_VERSION_AVTAB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 			rc = ebitmap_read(e, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 		/* add the type itself as the degenerate case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 		rc = ebitmap_set_bit(e, i, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	rc = policydb_bounds_sanity_check(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 	kfree(rtk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	kfree(rtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 	policydb_destroy(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726)  * Write a MLS level structure to a policydb binary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727)  * representation file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) static int mls_write_level(struct mls_level *l, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	__le32 buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	buf[0] = cpu_to_le32(l->sens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 	rc = ebitmap_write(&l->cat, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747)  * Write a MLS range structure to a policydb binary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748)  * representation file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) static int mls_write_range_helper(struct mls_range *r, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 	size_t items;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	int rc, eq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	eq = mls_level_eq(&r->level[1], &r->level[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	if (eq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 		items = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 		items = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 	buf[0] = cpu_to_le32(items-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	buf[1] = cpu_to_le32(r->level[0].sens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	if (!eq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 		buf[2] = cpu_to_le32(r->level[1].sens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 	BUG_ON(items > ARRAY_SIZE(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 	rc = put_entry(buf, sizeof(u32), items, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 	rc = ebitmap_write(&r->level[0].cat, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 	if (!eq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 		rc = ebitmap_write(&r->level[1].cat, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) static int sens_write(void *vkey, void *datum, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	char *key = vkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 	struct level_datum *levdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 	struct policy_data *pd = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 	void *fp = pd->fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 	__le32 buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 	len = strlen(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 	buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	buf[1] = cpu_to_le32(levdatum->isalias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 	rc = put_entry(buf, sizeof(u32), 2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 	rc = put_entry(key, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 	rc = mls_write_level(levdatum->level, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) static int cat_write(void *vkey, void *datum, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	char *key = vkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	struct cat_datum *catdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	struct policy_data *pd = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 	void *fp = pd->fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 	len = strlen(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 	buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 	buf[1] = cpu_to_le32(catdatum->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 	buf[2] = cpu_to_le32(catdatum->isalias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 	rc = put_entry(buf, sizeof(u32), 3, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 	rc = put_entry(key, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) static int role_trans_write_one(void *key, void *datum, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 	struct role_trans_key *rtk = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 	struct role_trans_datum *rtd = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	struct policy_data *pd = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 	void *fp = pd->fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	struct policydb *p = pd->p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 	buf[0] = cpu_to_le32(rtk->role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 	buf[1] = cpu_to_le32(rtk->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 	buf[2] = cpu_to_le32(rtd->new_role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 	rc = put_entry(buf, sizeof(u32), 3, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	if (p->policyvers >= POLICYDB_VERSION_ROLETRANS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 		buf[0] = cpu_to_le32(rtk->tclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 		rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) static int role_trans_write(struct policydb *p, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 	struct policy_data pd = { .p = p, .fp = fp };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 	__le32 buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 	buf[0] = cpu_to_le32(p->role_tr.nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 	rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 	return hashtab_map(&p->role_tr, role_trans_write_one, &pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) static int role_allow_write(struct role_allow *r, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 	struct role_allow *ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 	__le32 buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 	size_t nel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 	nel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 	for (ra = r; ra; ra = ra->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 		nel++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 	buf[0] = cpu_to_le32(nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 	rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 	for (ra = r; ra; ra = ra->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 		buf[0] = cpu_to_le32(ra->role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 		buf[1] = cpu_to_le32(ra->new_role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 		rc = put_entry(buf, sizeof(u32), 2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902)  * Write a security context structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903)  * to a policydb binary representation file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) static int context_write(struct policydb *p, struct context *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 			 void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 	buf[0] = cpu_to_le32(c->user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 	buf[1] = cpu_to_le32(c->role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 	buf[2] = cpu_to_le32(c->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 	rc = put_entry(buf, sizeof(u32), 3, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 	rc = mls_write_range_helper(&c->range, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927)  * The following *_write functions are used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928)  * write the symbol data to a policy database
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929)  * binary representation file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) static int perm_write(void *vkey, void *datum, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 	char *key = vkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 	struct perm_datum *perdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 	__le32 buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 	len = strlen(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 	buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 	buf[1] = cpu_to_le32(perdatum->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 	rc = put_entry(buf, sizeof(u32), 2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 	rc = put_entry(key, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) static int common_write(void *vkey, void *datum, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 	char *key = vkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 	struct common_datum *comdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 	struct policy_data *pd = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 	void *fp = pd->fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	__le32 buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 	len = strlen(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 	buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 	buf[1] = cpu_to_le32(comdatum->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 	buf[2] = cpu_to_le32(comdatum->permissions.nprim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 	buf[3] = cpu_to_le32(comdatum->permissions.table.nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 	rc = put_entry(buf, sizeof(u32), 4, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 	rc = put_entry(key, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 	rc = hashtab_map(&comdatum->permissions.table, perm_write, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) static int type_set_write(struct type_set *t, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 	__le32 buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 	if (ebitmap_write(&t->types, fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 	if (ebitmap_write(&t->negset, fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 	buf[0] = cpu_to_le32(t->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 	rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) static int write_cons_helper(struct policydb *p, struct constraint_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 			     void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 	struct constraint_node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 	struct constraint_expr *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 	u32 nel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 	for (c = node; c; c = c->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 		nel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 		for (e = c->expr; e; e = e->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 			nel++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 		buf[0] = cpu_to_le32(c->permissions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 		buf[1] = cpu_to_le32(nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 		rc = put_entry(buf, sizeof(u32), 2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 		for (e = c->expr; e; e = e->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 			buf[0] = cpu_to_le32(e->expr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 			buf[1] = cpu_to_le32(e->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 			buf[2] = cpu_to_le32(e->op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 			rc = put_entry(buf, sizeof(u32), 3, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 			switch (e->expr_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 			case CEXPR_NAMES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 				rc = ebitmap_write(&e->names, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 				if (p->policyvers >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 					POLICYDB_VERSION_CONSTRAINT_NAMES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 					rc = type_set_write(e->type_names, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 					if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 						return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) static int class_write(void *vkey, void *datum, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 	char *key = vkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 	struct class_datum *cladatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 	struct policy_data *pd = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 	void *fp = pd->fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 	struct policydb *p = pd->p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 	struct constraint_node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 	__le32 buf[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	u32 ncons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	size_t len, len2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 	len = strlen(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 	if (cladatum->comkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 		len2 = strlen(cladatum->comkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 		len2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 	ncons = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 	for (c = cladatum->constraints; c; c = c->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 		ncons++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 	buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 	buf[1] = cpu_to_le32(len2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 	buf[2] = cpu_to_le32(cladatum->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 	buf[3] = cpu_to_le32(cladatum->permissions.nprim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 	buf[4] = cpu_to_le32(cladatum->permissions.table.nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 	buf[5] = cpu_to_le32(ncons);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 	rc = put_entry(buf, sizeof(u32), 6, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 	rc = put_entry(key, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 	if (cladatum->comkey) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 		rc = put_entry(cladatum->comkey, 1, len2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 	rc = hashtab_map(&cladatum->permissions.table, perm_write, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 	rc = write_cons_helper(p, cladatum->constraints, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 	/* write out the validatetrans rule */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 	ncons = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 	for (c = cladatum->validatetrans; c; c = c->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 		ncons++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 	buf[0] = cpu_to_le32(ncons);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 	rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 	rc = write_cons_helper(p, cladatum->validatetrans, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 	if (p->policyvers >= POLICYDB_VERSION_NEW_OBJECT_DEFAULTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 		buf[0] = cpu_to_le32(cladatum->default_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 		buf[1] = cpu_to_le32(cladatum->default_role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 		buf[2] = cpu_to_le32(cladatum->default_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 		rc = put_entry(buf, sizeof(uint32_t), 3, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 	if (p->policyvers >= POLICYDB_VERSION_DEFAULT_TYPE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 		buf[0] = cpu_to_le32(cladatum->default_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 		rc = put_entry(buf, sizeof(uint32_t), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) static int role_write(void *vkey, void *datum, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 	char *key = vkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 	struct role_datum *role = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 	struct policy_data *pd = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 	void *fp = pd->fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 	struct policydb *p = pd->p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 	size_t items, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 	len = strlen(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 	items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 	buf[items++] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 	buf[items++] = cpu_to_le32(role->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 		buf[items++] = cpu_to_le32(role->bounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 	BUG_ON(items > ARRAY_SIZE(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 	rc = put_entry(buf, sizeof(u32), items, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 	rc = put_entry(key, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 	rc = ebitmap_write(&role->dominates, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 	rc = ebitmap_write(&role->types, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) static int type_write(void *vkey, void *datum, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) 	char *key = vkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 	struct type_datum *typdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 	struct policy_data *pd = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 	struct policydb *p = pd->p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 	void *fp = pd->fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 	__le32 buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 	size_t items, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 	len = strlen(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 	items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 	buf[items++] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 	buf[items++] = cpu_to_le32(typdatum->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 		u32 properties = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 		if (typdatum->primary)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 			properties |= TYPEDATUM_PROPERTY_PRIMARY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 		if (typdatum->attribute)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 			properties |= TYPEDATUM_PROPERTY_ATTRIBUTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 		buf[items++] = cpu_to_le32(properties);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 		buf[items++] = cpu_to_le32(typdatum->bounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 		buf[items++] = cpu_to_le32(typdatum->primary);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 	BUG_ON(items > ARRAY_SIZE(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 	rc = put_entry(buf, sizeof(u32), items, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 	rc = put_entry(key, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) static int user_write(void *vkey, void *datum, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 	char *key = vkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 	struct user_datum *usrdatum = datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 	struct policy_data *pd = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 	struct policydb *p = pd->p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 	void *fp = pd->fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 	size_t items, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 	len = strlen(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 	items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 	buf[items++] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 	buf[items++] = cpu_to_le32(usrdatum->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 	if (p->policyvers >= POLICYDB_VERSION_BOUNDARY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 		buf[items++] = cpu_to_le32(usrdatum->bounds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 	BUG_ON(items > ARRAY_SIZE(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 	rc = put_entry(buf, sizeof(u32), items, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 	rc = put_entry(key, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 	rc = ebitmap_write(&usrdatum->roles, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 	rc = mls_write_range_helper(&usrdatum->range, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 	rc = mls_write_level(&usrdatum->dfltlevel, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) static int (*write_f[SYM_NUM]) (void *key, void *datum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 				void *datap) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 	common_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 	class_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 	role_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 	type_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 	user_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 	cond_write_bool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 	sens_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 	cat_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 			  void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 	unsigned int i, j, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 	size_t nel, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 	__be64 prefixbuf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 	u32 nodebuf[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 	struct ocontext *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 	for (i = 0; i < info->ocon_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 		nel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 		for (c = p->ocontexts[i]; c; c = c->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 			nel++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 		buf[0] = cpu_to_le32(nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) 		rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 		for (c = p->ocontexts[i]; c; c = c->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 			switch (i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 			case OCON_ISID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 				buf[0] = cpu_to_le32(c->sid[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 				rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 				rc = context_write(p, &c->context[0], fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 			case OCON_FS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 			case OCON_NETIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 				len = strlen(c->u.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 				buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 				rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 				rc = put_entry(c->u.name, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 				rc = context_write(p, &c->context[0], fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 				rc = context_write(p, &c->context[1], fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 			case OCON_PORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 				buf[0] = cpu_to_le32(c->u.port.protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 				buf[1] = cpu_to_le32(c->u.port.low_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 				buf[2] = cpu_to_le32(c->u.port.high_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 				rc = put_entry(buf, sizeof(u32), 3, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 				rc = context_write(p, &c->context[0], fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 			case OCON_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 				nodebuf[0] = c->u.node.addr; /* network order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 				nodebuf[1] = c->u.node.mask; /* network order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 				rc = put_entry(nodebuf, sizeof(u32), 2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 				rc = context_write(p, &c->context[0], fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 			case OCON_FSUSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 				buf[0] = cpu_to_le32(c->v.behavior);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) 				len = strlen(c->u.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 				buf[1] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 				rc = put_entry(buf, sizeof(u32), 2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 				rc = put_entry(c->u.name, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 				rc = context_write(p, &c->context[0], fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 			case OCON_NODE6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 				for (j = 0; j < 4; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) 					nodebuf[j] = c->u.node6.addr[j]; /* network order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 				for (j = 0; j < 4; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 					nodebuf[j + 4] = c->u.node6.mask[j]; /* network order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 				rc = put_entry(nodebuf, sizeof(u32), 8, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) 				rc = context_write(p, &c->context[0], fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) 			case OCON_IBPKEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 				/* subnet_prefix is in CPU order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 				prefixbuf[0] = cpu_to_be64(c->u.ibpkey.subnet_prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 				rc = put_entry(prefixbuf, sizeof(u64), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 				buf[0] = cpu_to_le32(c->u.ibpkey.low_pkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 				buf[1] = cpu_to_le32(c->u.ibpkey.high_pkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 				rc = put_entry(buf, sizeof(u32), 2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 				rc = context_write(p, &c->context[0], fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 			case OCON_IBENDPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 				len = strlen(c->u.ibendport.dev_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 				buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 				buf[1] = cpu_to_le32(c->u.ibendport.port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 				rc = put_entry(buf, sizeof(u32), 2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 				rc = put_entry(c->u.ibendport.dev_name, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 				rc = context_write(p, &c->context[0], fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) static int genfs_write(struct policydb *p, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 	struct genfs *genfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 	struct ocontext *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 	__le32 buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 	len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 	for (genfs = p->genfs; genfs; genfs = genfs->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 		len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 	buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 	rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 	for (genfs = p->genfs; genfs; genfs = genfs->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 		len = strlen(genfs->fstype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 		buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) 		rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) 		rc = put_entry(genfs->fstype, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 		len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 		for (c = genfs->head; c; c = c->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 			len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 		buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 		rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) 		for (c = genfs->head; c; c = c->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 			len = strlen(c->u.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 			buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 			rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) 			rc = put_entry(c->u.name, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) 			buf[0] = cpu_to_le32(c->v.sclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) 			rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 			rc = context_write(p, &c->context[0], fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) static int range_write_helper(void *key, void *data, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 	__le32 buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 	struct range_trans *rt = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 	struct mls_range *r = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 	struct policy_data *pd = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 	void *fp = pd->fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 	struct policydb *p = pd->p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 	buf[0] = cpu_to_le32(rt->source_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 	buf[1] = cpu_to_le32(rt->target_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 	rc = put_entry(buf, sizeof(u32), 2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 	if (p->policyvers >= POLICYDB_VERSION_RANGETRANS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 		buf[0] = cpu_to_le32(rt->target_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 		rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 	rc = mls_write_range_helper(r, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) static int range_write(struct policydb *p, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 	__le32 buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) 	struct policy_data pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 	pd.p = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) 	pd.fp = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 	buf[0] = cpu_to_le32(p->range_tr.nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 	rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 	/* actually write all of the entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 	rc = hashtab_map(&p->range_tr, range_write_helper, &pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) static int filename_write_helper_compat(void *key, void *data, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 	struct filename_trans_key *ft = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 	struct filename_trans_datum *datum = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 	struct ebitmap_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 	void *fp = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 	__le32 buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 	u32 bit, len = strlen(ft->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 		ebitmap_for_each_positive_bit(&datum->stypes, node, bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 			buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 			rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 			rc = put_entry(ft->name, sizeof(char), len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 			buf[0] = cpu_to_le32(bit + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) 			buf[1] = cpu_to_le32(ft->ttype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) 			buf[2] = cpu_to_le32(ft->tclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) 			buf[3] = cpu_to_le32(datum->otype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) 			rc = put_entry(buf, sizeof(u32), 4, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) 		datum = datum->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) 	} while (unlikely(datum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) static int filename_write_helper(void *key, void *data, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 	struct filename_trans_key *ft = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) 	struct filename_trans_datum *datum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) 	void *fp = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 	__le32 buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 	u32 ndatum, len = strlen(ft->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 	buf[0] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 	rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 	rc = put_entry(ft->name, sizeof(char), len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 	ndatum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 	datum = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 		ndatum++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 		datum = datum->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 	} while (unlikely(datum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 	buf[0] = cpu_to_le32(ft->ttype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 	buf[1] = cpu_to_le32(ft->tclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 	buf[2] = cpu_to_le32(ndatum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 	rc = put_entry(buf, sizeof(u32), 3, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 	datum = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 		rc = ebitmap_write(&datum->stypes, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) 		buf[0] = cpu_to_le32(datum->otype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) 		rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 		datum = datum->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 	} while (unlikely(datum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) static int filename_trans_write(struct policydb *p, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 	__le32 buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 	if (p->policyvers < POLICYDB_VERSION_FILENAME_TRANS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 	if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 		buf[0] = cpu_to_le32(p->compat_filename_trans_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 		rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) 		rc = hashtab_map(&p->filename_trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 				 filename_write_helper_compat, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 		buf[0] = cpu_to_le32(p->filename_trans.nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 		rc = put_entry(buf, sizeof(u32), 1, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) 		rc = hashtab_map(&p->filename_trans, filename_write_helper, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614)  * Write the configuration data in a policy database
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615)  * structure to a policy database binary representation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616)  * file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) int policydb_write(struct policydb *p, void *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 	unsigned int i, num_syms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 	__le32 buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 	u32 config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 	struct policydb_compat_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 	 * refuse to write policy older than compressed avtab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 	 * to simplify the writer.  There are other tests dropped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 	 * since we assume this throughout the writer code.  Be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 	 * careful if you ever try to remove this restriction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 	if (p->policyvers < POLICYDB_VERSION_AVTAB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 		pr_err("SELinux: refusing to write policy version %d."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 		       "  Because it is less than version %d\n", p->policyvers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 		       POLICYDB_VERSION_AVTAB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 	config = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 	if (p->mls_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 		config |= POLICYDB_CONFIG_MLS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 	if (p->reject_unknown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 		config |= REJECT_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 	if (p->allow_unknown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 		config |= ALLOW_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 	/* Write the magic number and string identifiers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 	buf[0] = cpu_to_le32(POLICYDB_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 	len = strlen(POLICYDB_STRING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 	buf[1] = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 	rc = put_entry(buf, sizeof(u32), 2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 	rc = put_entry(POLICYDB_STRING, 1, len, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 	/* Write the version, config, and table sizes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 	info = policydb_lookup_compat(p->policyvers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 	if (!info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 		pr_err("SELinux: compatibility lookup failed for policy "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 		    "version %d", p->policyvers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 	buf[0] = cpu_to_le32(p->policyvers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 	buf[1] = cpu_to_le32(config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 	buf[2] = cpu_to_le32(info->sym_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 	buf[3] = cpu_to_le32(info->ocon_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 	rc = put_entry(buf, sizeof(u32), 4, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 	if (p->policyvers >= POLICYDB_VERSION_POLCAP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) 		rc = ebitmap_write(&p->policycaps, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 	if (p->policyvers >= POLICYDB_VERSION_PERMISSIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 		rc = ebitmap_write(&p->permissive_map, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 	num_syms = info->sym_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 	for (i = 0; i < num_syms; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) 		struct policy_data pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) 		pd.fp = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 		pd.p = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) 		buf[0] = cpu_to_le32(p->symtab[i].nprim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) 		buf[1] = cpu_to_le32(p->symtab[i].table.nel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 		rc = put_entry(buf, sizeof(u32), 2, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 		rc = hashtab_map(&p->symtab[i].table, write_f[i], &pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 	rc = avtab_write(p, &p->te_avtab, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 	rc = cond_write_list(p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 	rc = role_trans_write(p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 	rc = role_allow_write(p->role_allow, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 	rc = filename_trans_write(p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 	rc = ocontext_write(p, info, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) 	rc = genfs_write(p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) 	rc = range_write(p, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 	for (i = 0; i < p->p_types.nprim; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) 		struct ebitmap *e = &p->type_attr_map_array[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 		rc = ebitmap_write(e, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) }