Commit b07e3c3a authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'for-2.6.20' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/selinux-2.6

* 'for-2.6.20' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/selinux-2.6:
  SELinux: validate kernel object classes and permissions
  SELinux: ensure keys constant in hashtab_search
  SELinux: export object class and permission definitions
  SELinux: remove current object class and permission validation mechanism
parents 5f56bbdf b94c7e67
...@@ -32,12 +32,7 @@ ...@@ -32,12 +32,7 @@
#include "avc.h" #include "avc.h"
#include "avc_ss.h" #include "avc_ss.h"
static const struct av_perm_to_string static const struct av_perm_to_string av_perm_to_string[] = {
{
u16 tclass;
u32 value;
const char *name;
} av_perm_to_string[] = {
#define S_(c, v, s) { c, v, s }, #define S_(c, v, s) { c, v, s },
#include "av_perm_to_string.h" #include "av_perm_to_string.h"
#undef S_ #undef S_
...@@ -57,17 +52,21 @@ static const char *class_to_string[] = { ...@@ -57,17 +52,21 @@ static const char *class_to_string[] = {
#undef TE_ #undef TE_
#undef S_ #undef S_
static const struct av_inherit static const struct av_inherit av_inherit[] = {
{
u16 tclass;
const char **common_pts;
u32 common_base;
} av_inherit[] = {
#define S_(c, i, b) { c, common_##i##_perm_to_string, b }, #define S_(c, i, b) { c, common_##i##_perm_to_string, b },
#include "av_inherit.h" #include "av_inherit.h"
#undef S_ #undef S_
}; };
const struct selinux_class_perm selinux_class_perm = {
av_perm_to_string,
ARRAY_SIZE(av_perm_to_string),
class_to_string,
ARRAY_SIZE(class_to_string),
av_inherit,
ARRAY_SIZE(av_inherit)
};
#define AVC_CACHE_SLOTS 512 #define AVC_CACHE_SLOTS 512
#define AVC_DEF_CACHE_THRESHOLD 512 #define AVC_DEF_CACHE_THRESHOLD 512
#define AVC_CACHE_RECLAIM 16 #define AVC_CACHE_RECLAIM 16
......
...@@ -10,5 +10,29 @@ ...@@ -10,5 +10,29 @@
int avc_ss_reset(u32 seqno); int avc_ss_reset(u32 seqno);
struct av_perm_to_string
{
u16 tclass;
u32 value;
const char *name;
};
struct av_inherit
{
u16 tclass;
const char **common_pts;
u32 common_base;
};
struct selinux_class_perm
{
const struct av_perm_to_string *av_perm_to_string;
u32 av_pts_len;
const char **class_to_string;
u32 cts_len;
const struct av_inherit *av_inherit;
u32 av_inherit_len;
};
#endif /* _SELINUX_AVC_SS_H_ */ #endif /* _SELINUX_AVC_SS_H_ */
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
#include <linux/errno.h> #include <linux/errno.h>
#include "hashtab.h" #include "hashtab.h"
struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, void *key), struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
int (*keycmp)(struct hashtab *h, void *key1, void *key2), int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
u32 size) u32 size)
{ {
struct hashtab *p; struct hashtab *p;
...@@ -71,7 +71,7 @@ int hashtab_insert(struct hashtab *h, void *key, void *datum) ...@@ -71,7 +71,7 @@ int hashtab_insert(struct hashtab *h, void *key, void *datum)
return 0; return 0;
} }
void *hashtab_search(struct hashtab *h, void *key) void *hashtab_search(struct hashtab *h, const void *key)
{ {
u32 hvalue; u32 hvalue;
struct hashtab_node *cur; struct hashtab_node *cur;
......
...@@ -22,9 +22,9 @@ struct hashtab { ...@@ -22,9 +22,9 @@ struct hashtab {
struct hashtab_node **htable; /* hash table */ struct hashtab_node **htable; /* hash table */
u32 size; /* number of slots in hash table */ u32 size; /* number of slots in hash table */
u32 nel; /* number of elements in hash table */ u32 nel; /* number of elements in hash table */
u32 (*hash_value)(struct hashtab *h, void *key); u32 (*hash_value)(struct hashtab *h, const void *key);
/* hash function */ /* hash function */
int (*keycmp)(struct hashtab *h, void *key1, void *key2); int (*keycmp)(struct hashtab *h, const void *key1, const void *key2);
/* key comparison function */ /* key comparison function */
}; };
...@@ -39,8 +39,8 @@ struct hashtab_info { ...@@ -39,8 +39,8 @@ struct hashtab_info {
* Returns NULL if insufficent space is available or * Returns NULL if insufficent space is available or
* the new hash table otherwise. * the new hash table otherwise.
*/ */
struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, void *key), struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
int (*keycmp)(struct hashtab *h, void *key1, void *key2), int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
u32 size); u32 size);
/* /*
...@@ -59,7 +59,7 @@ int hashtab_insert(struct hashtab *h, void *k, void *d); ...@@ -59,7 +59,7 @@ int hashtab_insert(struct hashtab *h, void *k, void *d);
* Returns NULL if no entry has the specified key or * Returns NULL if no entry has the specified key or
* the datum of the entry otherwise. * the datum of the entry otherwise.
*/ */
void *hashtab_search(struct hashtab *h, void *k); void *hashtab_search(struct hashtab *h, const void *k);
/* /*
* Destroys the specified hash table. * Destroys the specified hash table.
......
...@@ -17,9 +17,13 @@ ...@@ -17,9 +17,13 @@
* *
* Added support for NetLabel * Added support for NetLabel
* *
* Updated: Chad Sellers <csellers@tresys.com>
*
* Added validation of kernel classes and permissions
*
* Copyright (C) 2006 Hewlett-Packard Development Company, L.P. * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
* Copyright (C) 2004-2006 Trusted Computer Solutions, Inc. * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
* Copyright (C) 2003 - 2004 Tresys Technology, LLC * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
* Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com> * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
...@@ -53,6 +57,11 @@ ...@@ -53,6 +57,11 @@
extern void selnl_notify_policyload(u32 seqno); extern void selnl_notify_policyload(u32 seqno);
unsigned int policydb_loaded_version; unsigned int policydb_loaded_version;
/*
* This is declared in avc.c
*/
extern const struct selinux_class_perm selinux_class_perm;
static DEFINE_RWLOCK(policy_rwlock); static DEFINE_RWLOCK(policy_rwlock);
#define POLICY_RDLOCK read_lock(&policy_rwlock) #define POLICY_RDLOCK read_lock(&policy_rwlock)
#define POLICY_WRLOCK write_lock_irq(&policy_rwlock) #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
...@@ -1019,86 +1028,112 @@ int security_change_sid(u32 ssid, ...@@ -1019,86 +1028,112 @@ int security_change_sid(u32 ssid,
} }
/* /*
* Verify that each permission that is defined under the * Verify that each kernel class that is defined in the
* existing policy is still defined with the same value * policy is correct
* in the new policy.
*/ */
static int validate_perm(void *key, void *datum, void *p) static int validate_classes(struct policydb *p)
{ {
struct hashtab *h; int i, j;
struct perm_datum *perdatum, *perdatum2; struct class_datum *cladatum;
int rc = 0; struct perm_datum *perdatum;
u32 nprim, tmp, common_pts_len, perm_val, pol_val;
u16 class_val;
h = p; const struct selinux_class_perm *kdefs = &selinux_class_perm;
perdatum = datum; const char *def_class, *def_perm, *pol_class;
struct symtab *perms;
perdatum2 = hashtab_search(h, key);
if (!perdatum2) { for (i = 1; i < kdefs->cts_len; i++) {
printk(KERN_ERR "security: permission %s disappeared", def_class = kdefs->class_to_string[i];
(char *)key); if (i > p->p_classes.nprim) {
rc = -ENOENT; printk(KERN_INFO
goto out; "security: class %s not defined in policy\n",
def_class);
continue;
} }
if (perdatum->value != perdatum2->value) { pol_class = p->p_class_val_to_name[i-1];
printk(KERN_ERR "security: the value of permission %s changed", if (strcmp(pol_class, def_class)) {
(char *)key); printk(KERN_ERR
rc = -EINVAL; "security: class %d is incorrect, found %s but should be %s\n",
i, pol_class, def_class);
return -EINVAL;
} }
out:
return rc;
}
/*
* Verify that each class that is defined under the
* existing policy is still defined with the same
* attributes in the new policy.
*/
static int validate_class(void *key, void *datum, void *p)
{
struct policydb *newp;
struct class_datum *cladatum, *cladatum2;
int rc;
newp = p;
cladatum = datum;
cladatum2 = hashtab_search(newp->p_classes.table, key);
if (!cladatum2) {
printk(KERN_ERR "security: class %s disappeared\n",
(char *)key);
rc = -ENOENT;
goto out;
} }
if (cladatum->value != cladatum2->value) { for (i = 0; i < kdefs->av_pts_len; i++) {
printk(KERN_ERR "security: the value of class %s changed\n", class_val = kdefs->av_perm_to_string[i].tclass;
(char *)key); perm_val = kdefs->av_perm_to_string[i].value;
rc = -EINVAL; def_perm = kdefs->av_perm_to_string[i].name;
goto out; if (class_val > p->p_classes.nprim)
continue;
pol_class = p->p_class_val_to_name[class_val-1];
cladatum = hashtab_search(p->p_classes.table, pol_class);
BUG_ON(!cladatum);
perms = &cladatum->permissions;
nprim = 1 << (perms->nprim - 1);
if (perm_val > nprim) {
printk(KERN_INFO
"security: permission %s in class %s not defined in policy\n",
def_perm, pol_class);
continue;
} }
if ((cladatum->comdatum && !cladatum2->comdatum) || perdatum = hashtab_search(perms->table, def_perm);
(!cladatum->comdatum && cladatum2->comdatum)) { if (perdatum == NULL) {
printk(KERN_ERR "security: the inherits clause for the access " printk(KERN_ERR
"vector definition for class %s changed\n", (char *)key); "security: permission %s in class %s not found in policy\n",
rc = -EINVAL; def_perm, pol_class);
goto out; return -EINVAL;
} }
if (cladatum->comdatum) { pol_val = 1 << (perdatum->value - 1);
rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm, if (pol_val != perm_val) {
cladatum2->comdatum->permissions.table); printk(KERN_ERR
if (rc) { "security: permission %s in class %s has incorrect value\n",
printk(" in the access vector definition for class " def_perm, pol_class);
"%s\n", (char *)key); return -EINVAL;
goto out;
} }
} }
rc = hashtab_map(cladatum->permissions.table, validate_perm, for (i = 0; i < kdefs->av_inherit_len; i++) {
cladatum2->permissions.table); class_val = kdefs->av_inherit[i].tclass;
if (rc) if (class_val > p->p_classes.nprim)
printk(" in access vector definition for class %s\n", continue;
(char *)key); pol_class = p->p_class_val_to_name[class_val-1];
out: cladatum = hashtab_search(p->p_classes.table, pol_class);
return rc; BUG_ON(!cladatum);
if (!cladatum->comdatum) {
printk(KERN_ERR
"security: class %s should have an inherits clause but does not\n",
pol_class);
return -EINVAL;
}
tmp = kdefs->av_inherit[i].common_base;
common_pts_len = 0;
while (!(tmp & 0x01)) {
common_pts_len++;
tmp >>= 1;
}
perms = &cladatum->comdatum->permissions;
for (j = 0; j < common_pts_len; j++) {
def_perm = kdefs->av_inherit[i].common_pts[j];
if (j >= perms->nprim) {
printk(KERN_INFO
"security: permission %s in class %s not defined in policy\n",
def_perm, pol_class);
continue;
}
perdatum = hashtab_search(perms->table, def_perm);
if (perdatum == NULL) {
printk(KERN_ERR
"security: permission %s in class %s not found in policy\n",
def_perm, pol_class);
return -EINVAL;
}
if (perdatum->value != j + 1) {
printk(KERN_ERR
"security: permission %s in class %s has incorrect value\n",
def_perm, pol_class);
return -EINVAL;
}
}
}
return 0;
} }
/* Clone the SID into the new SID table. */ /* Clone the SID into the new SID table. */
...@@ -1243,6 +1278,16 @@ int security_load_policy(void *data, size_t len) ...@@ -1243,6 +1278,16 @@ int security_load_policy(void *data, size_t len)
avtab_cache_destroy(); avtab_cache_destroy();
return -EINVAL; return -EINVAL;
} }
/* Verify that the kernel defined classes are correct. */
if (validate_classes(&policydb)) {
printk(KERN_ERR
"security: the definition of a class is incorrect\n");
LOAD_UNLOCK;
sidtab_destroy(&sidtab);
policydb_destroy(&policydb);
avtab_cache_destroy();
return -EINVAL;
}
policydb_loaded_version = policydb.policyvers; policydb_loaded_version = policydb.policyvers;
ss_initialized = 1; ss_initialized = 1;
seqno = ++latest_granting; seqno = ++latest_granting;
...@@ -1265,10 +1310,10 @@ int security_load_policy(void *data, size_t len) ...@@ -1265,10 +1310,10 @@ int security_load_policy(void *data, size_t len)
sidtab_init(&newsidtab); sidtab_init(&newsidtab);
/* Verify that the existing classes did not change. */ /* Verify that the kernel defined classes are correct. */
if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) { if (validate_classes(&newpolicydb)) {
printk(KERN_ERR "security: the definition of an existing " printk(KERN_ERR
"class changed\n"); "security: the definition of a class is incorrect\n");
rc = -EINVAL; rc = -EINVAL;
goto err; goto err;
} }
......
...@@ -9,9 +9,9 @@ ...@@ -9,9 +9,9 @@
#include <linux/errno.h> #include <linux/errno.h>
#include "symtab.h" #include "symtab.h"
static unsigned int symhash(struct hashtab *h, void *key) static unsigned int symhash(struct hashtab *h, const void *key)
{ {
char *p, *keyp; const char *p, *keyp;
unsigned int size; unsigned int size;
unsigned int val; unsigned int val;
...@@ -23,9 +23,9 @@ static unsigned int symhash(struct hashtab *h, void *key) ...@@ -23,9 +23,9 @@ static unsigned int symhash(struct hashtab *h, void *key)
return val & (h->size - 1); return val & (h->size - 1);
} }
static int symcmp(struct hashtab *h, void *key1, void *key2) static int symcmp(struct hashtab *h, const void *key1, const void *key2)
{ {
char *keyp1, *keyp2; const char *keyp1, *keyp2;
keyp1 = key1; keyp1 = key1;
keyp2 = key2; keyp2 = key2;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment