Skip to contentSkip to conversation

Improve loading performance by 80% using dynamic resizing

PATCH In review Matthew Robertson 3 participants 6 messages Jul 29, 2010 – Aug 4, 2010 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improve loading performance by 80% using dynamic resizing

After spending a lot of developer time recently doing repeated relinks of policies after making minor changes we put some time into tracking down avenues for improving performance in the SELinux libraries. On a reasonably modern HP server a 'make load' of a precompiled SELinux Reference Policy takes roughly 96 seconds. The 'perf' tool pointed us to some undersized and overpopulated hash tables in libsepol's hashtab.c and avtab.c. By implementing dynamic resizing for those structures we were able to bring that down to a 19 second runtime (80% performance improvement) at the cost of ~6% more memory overhead.

---

After applying this fix, bzip2 takes up a disproportionate percentage of the time for a policy load. By effectively disabling bzip2 (changing bzip_blocksize in libsemanage to 0) we reduced load time to about 11 seconds--disabling compression might not be a tenable solution but supporting a faster compression option like gzip could be worth the trouble if the other fixes check out.

For reference, here are the numbers and perf reports we're looking at for the policy loading step after a compile/install:

SELinux Reference Policy        v2.20091117
libsepol                        v2.0.41
libsemanage                     v2.0.45
libselinux                      v2.0.94
libbz2                          v1.0.5-4

ORIGINAL LIBRARIES: 96.0s
[full compile + load: 142s]

    54.34%  semodule  /lib/libc-2.11.2.so            [.] __strcmp_sse42
    16.08%  semodule  /lib/libsepol.so.1             [.] hashtab_search
     4.74%  semodule  /lib/libsepol.so.1             [.] symcmp
     3.98%  semodule  /lib/libsepol.so.1             [.] strcmp@plt
     3.76%  semodule  /lib/libbz2.so.1.0.4           [.] 0x0000000000b0bc
     2.80%  semodule  /lib/libsepol.so.1             [.] avtab_search_node
     2.72%  semodule  /lib/libbz2.so.1.0.4           [.] 0x000000000022d0
     1.51%  semodule  /lib/libsepol.so.1             [.] ebitmap_set_bit
     1.12%  semodule  /lib/libsepol.so.1             [.] hashtab_map
     1.00%  semodule  /lib/libsepol.so.1             [.] symhash
     0.95%  semodule  /lib/libbz2.so.1.0.4           [.] BZ2_decompress
     0.90%  semodule  /lib/libbz2.so.1.0.4           [.] BZ2_compressBlock
     0.68%  semodule  /lib/libc-2.11.2.so            [.] __strlen_sse42
     0.57%  semodule  /lib/libbz2.so.1.0.4           [.] BZ2_bzDecompress

DYNAMIC HASHING ENABLED, BZIP2 LEFT ENABLED: 19s, 80% improvement [full compile + load: 70.5s, ~50% improvement]

    28.57%  semodule  /lib/libbz2.so.1.0.4           [.] 0x0000000000b539
     7.53%  semodule  /lib/libsepol.so.1             [.] ebitmap_set_bit
     7.24%  semodule  /lib/libc-2.11.2.so            [.] __strcmp_sse42
     5.91%  semodule  /lib/libsepol.so.1             [.] hashtab_map
     4.91%  semodule  /lib/libsepol.so.1             [.] symhash
     4.66%  semodule  /lib/libsepol.so.1             [.] hashtab_search
     4.58%  semodule  /lib/libbz2.so.1.0.4           [.] BZ2_decompress
     4.46%  semodule  /lib/libbz2.so.1.0.4           [.] BZ2_compressBlock
     4.38%  semodule  /lib/libsepol.so.1             [.] avtab_search_node
     3.40%  semodule  /lib/libbz2.so.1.0.4           [.] 0x0000000000d1f9
     2.29%  semodule  /lib/libbz2.so.1.0.4           [.] BZ2_bzDecompress

DYNAMIC HASHING ENABLED, BZIP2 DISABLED: 11 seconds, 88% improvement [full compile + load: 62s, ~55% improvement]

    13.39%  semodule  /lib/libsepol.so.1             [.] ebitmap_set_bit
    12.77%  semodule  /lib/libc-2.11.2.so            [.] __strcmp_sse42
    10.44%  semodule  /lib/libsepol.so.1             [.] hashtab_map
     8.91%  semodule  /lib/libsepol.so.1             [.] symhash
     8.28%  semodule  /lib/libsepol.so.1             [.] hashtab_search
     7.74%  semodule  /lib/libsepol.so.1             [.] avtab_search_node
 libsepol/src/avtab.c   |   48 ++++++++++++++++++++++----
 libsepol/src/hashtab.c |   89 +++++++++++++++++++++++++++++++++--------------
 2 files changed, 103 insertions(+), 34 deletions(-)
libsepol/src/avtab.c +41 -7
index ea947cb..51abd04 100644
@@ -78,17 +78,13 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key,
 	return newnode;
 }
 
-int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum)
+int avtab_insert__(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum)
 {
-	int hvalue;
 	avtab_ptr_t prev, cur, newnode;
+	int hvalue = avtab_hash(key, h->mask);
 	uint16_t specified =
 	    key->specified & ~(AVTAB_ENABLED | AVTAB_ENABLED_OLD);
 
-	if (!h || !h->htable)
-		return SEPOL_ENOMEM;
-
-	hvalue = avtab_hash(key, h->mask);
 	for (prev = NULL, cur = h->htable[hvalue];
 	     cur; prev = cur, cur = cur->next) {
 		if (key->source_type == cur->key.source_type &&
@@ -110,8 +106,46 @@ int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum)
 	newnode = avtab_insert_node(h, hvalue, prev, key, datum);
 	if (!newnode)
 		return SEPOL_ENOMEM;
+	return SEPOL_OK;
+}
 
-	return 0;
+int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum)
+{
+        int oldsize, i;
+	avtab_ptr_t prev, cur, *oldtbl, *newtbl;
+
+	if (!h || !h->htable)
+		return SEPOL_ENOMEM;
+
+	/* If we don't need to resize, just insert the new entry */
+	if (h->nslot * 3/4 > h->nel)
+		return avtab_insert__(h, key, datum);
+
+	/* Attempt to allocate a bigger table */
+	newtbl = (avtab_ptr_t*) calloc(sizeof(avtab_ptr_t), h->nslot * 2);
+	if (newtbl == NULL)
+		return avtab_insert__(h, key, datum);
+
+	/* Reinsert all elements into the new table */
+	h->nel = 0;
+	oldsize = h->nslot;
+	h->nslot *= 2;
+	h->mask = h->nslot - 1;
+	oldtbl = h->htable;
+	h->htable = newtbl;
+
+	for (i = 0; i < oldsize; i++) {
+		cur = oldtbl[i];
+		while (cur != NULL) {
+			avtab_insert(h, &(cur->key), &(cur->datum));
+			prev = cur;
+			cur = cur->next;
+			free (prev);
+		}
+	}
+	free (oldtbl);
+
+	return avtab_insert__(h, key, datum);
 }
 
 /* Unlike avtab_insert(), this function allow multiple insertions of the same 
libsepol/src/hashtab.c +62 -27

103 lines not on this page · raw message

Signature · 7 lines
1.7.0


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#1 lore raw
Quoted 10 lines · Matthew Robertson

After spending a lot of developer time recently doing repeated relinks of policies after making minor changes we put some time into tracking down avenues for improving performance in the SELinux libraries. On a reasonably modern HP server a 'make load' of a precompiled SELinux Reference Policy takes roughly 96 seconds. The 'perf' tool pointed us to some undersized and overpopulated hash tables in libsepol's hashtab.c and avtab.c. By implementing dynamic resizing for those structures we were able to bring that down to a 19 second runtime (80% performance improvement) at the cost of ~6% more memory overhead.

Thanks for the patch. Some comments below will need to be addressed for merging.

libsepol/src/avtab.c avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key, · line 81

Quoted 10 lines
libsepol/src/avtab.c +1 -1
index ea947cb..51abd04 100644
@@ -78,17 +78,13 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key,
 	return newnode;
 }
 
-int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum)
+int avtab_insert__(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum)

This should be made a static function, and I think the convention is to use leading underscores rather than trailing ones for internal functions. Or use a unique suffix that has an actual meaning, e.g. avtab_insert_helper().

libsepol/src/avtab.c int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum) · line 121

Quoted 10 lines
libsepol/src/avtab.c +10 -0
+int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum)
+{
+        int oldsize, i;
+	avtab_ptr_t prev, cur, *oldtbl, *newtbl;
+
+	if (!h || !h->htable)
+		return SEPOL_ENOMEM;
+
+	/* If we don't need to resize, just insert the new entry */
+	if (h->nslot * 3/4 > h->nel)

The resizing threshold can be precomputed by avtab_alloc() and stored in the avtab as a field (e.g. h->threshold), so you can just compare without recomputing here. Why 3/4? That should likely be a #define or otherwise settable.

libsepol/src/avtab.c int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum) · line 125

Quoted 4 lines
libsepol/src/avtab.c +4 -0
+		return avtab_insert__(h, key, datum);
+
+	/* Attempt to allocate a bigger table */
+	newtbl = (avtab_ptr_t*) calloc(sizeof(avtab_ptr_t), h->nslot * 2);

Order of arguments to calloc() is reversed - this is an array with h->nslot*2 members of size sizeof(avtab_ptr_t) bytes each, not the other way around. Yields the same effect but should be fixed.

Quoted 5 lines
libsepol/src/avtab.c +5 -0
+	if (newtbl == NULL)
+		return avtab_insert__(h, key, datum);
+
+	/* Reinsert all elements into the new table */
+	h->nel = 0;

libsepol/src/avtab.c int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum) · line 140

Quoted 10 lines
libsepol/src/avtab.c +10 -0
+	oldsize = h->nslot;
+	h->nslot *= 2;
+	h->mask = h->nslot - 1;
+	oldtbl = h->htable;
+	h->htable = newtbl;
+
+	for (i = 0; i < oldsize; i++) {
+		cur = oldtbl[i];
+		while (cur != NULL) {
+			avtab_insert(h, &(cur->key), &(cur->datum));

avtab_insert() can fail, so at the least, you need to check and handle errors, particularly ENOMEM, or you can lose data here. Also, I think you want to call __avtab_insert() rather than recursively calling avtab_insert() here, as you shouldn't need to check for resizing the table here.
Last, we could avoid both the possibility of memory allocation failure and be more efficient by reusing the already existing avtab nodes and merely re-chaining them into the new hash table. This would however require writing a bit of custom code for doing that (or factoring out a common helper) rather than just reusing __avtab_insert().

libsepol/src/hashtab.c hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, · line 66

Quoted 9 lines
libsepol/src/hashtab.c +1 -0
index c4be72c..943a2cd 100644
@@ -63,41 +63,76 @@ hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h,
 	return p;
 }
 
+int hashtab_insert__(hashtab_t h, hashtab_key_t key, hashtab_datum_t datum)

As above, this should be made static, and either use leading underscores or a meaningful suffix.

libsepol/src/hashtab.c hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, · line 108

Quoted 2 lines
libsepol/src/hashtab.c +2 -0
+	/* If we don't need to resize, just insert the new entry */
+	if (h->size > h->nel)

resize threshold should be adjustable, at least via a #define, and can be precomputed during hashtab_create().

libsepol/src/hashtab.c hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, · line 112

Quoted 4 lines
libsepol/src/hashtab.c +4 -0
+		return hashtab_insert__(h, key, datum);
+
+	/* Attempt to allocate a bigger table */
+	newtbl = (hashtab_ptr_t*) calloc(sizeof(hashtab_ptr_t), h->size * 8);

calloc order of arguments is wrong again.

libsepol/src/hashtab.c hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, · line 120

Quoted 8 lines
libsepol/src/hashtab.c +8 -0
+	if (newtbl == NULL)
+		return hashtab_insert__(h, key, datum);
+
+	/* Reinsert all elements into the new table */
+	oldtbl = h->htable;
+	oldsize = h->size;
+	h->nel = 0;
+	h->size *= 8;

The factor should likely be definable. Why 8 here vs 2 in the avtab case?

libsepol/src/hashtab.c hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, · line 127

Quoted 7 lines
libsepol/src/hashtab.c +7 -0
+	h->htable = newtbl;
+
+	for (i = 0; i < oldsize; i++) {
+		hashtab_ptr_t prev = NULL;
+		hashtab_ptr_t cur = oldtbl[i];
+		while (cur != NULL) {
+			hashtab_insert__(h, cur->key, cur->datum);

This can fail. Same as in the avtab case. Optimal situation would be to re-chain the existing nodes into the new hashtab rather than allocating new nodes, copying the contents, and freeing the old ones.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#2 lore raw

Re: [PATCH] Improve loading performance by 80% using dynamicresizing

Thanks for the comments, Stephen--we'll do a rewrite to address the issues you've identified.

Quoted 121 lines · Stephen Smalley
Quoted 10 lines · Matthew Robertson

After spending a lot of developer time recently doing repeated relinks of policies after making minor changes we put some time into tracking down avenues for improving performance in the SELinux libraries. On a reasonably modern HP server a 'make load' of a precompiled SELinux Reference Policy takes roughly 96 seconds. The 'perf' tool pointed us to some undersized and overpopulated hash tables in libsepol's hashtab.c and avtab.c. By implementing dynamic resizing for those structures we were able to bring that down to a 19 second runtime (80% performance improvement) at the cost of ~6% more memory overhead.

Thanks for the patch. Some comments below will need to be addressed for merging.

libsepol/src/avtab.c avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key, · line 81

Quoted 10 lines
libsepol/src/avtab.c +1 -1
index ea947cb..51abd04 100644
@@ -78,17 +78,13 @@ avtab_insert_node(avtab_t * h, int hvalue, avtab_ptr_t prev, avtab_key_t * key,
 	return newnode;
 }
 
-int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum)
+int avtab_insert__(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum)

This should be made a static function, and I think the convention is to use leading underscores rather than trailing ones for internal functions. Or use a unique suffix that has an actual meaning, e.g. avtab_insert_helper().

libsepol/src/avtab.c int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum) · line 121

Quoted 10 lines
libsepol/src/avtab.c +10 -0
+int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum)
+{
+        int oldsize, i;
+	avtab_ptr_t prev, cur, *oldtbl, *newtbl;
+
+	if (!h || !h->htable)
+		return SEPOL_ENOMEM;
+
+	/* If we don't need to resize, just insert the new entry */
+	if (h->nslot * 3/4 > h->nel)

The resizing threshold can be precomputed by avtab_alloc() and stored in the avtab as a field (e.g. h->threshold), so you can just compare without recomputing here. Why 3/4? That should likely be a #define or otherwise settable.

libsepol/src/avtab.c int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum) · line 125

Quoted 4 lines
libsepol/src/avtab.c +4 -0
+		return avtab_insert__(h, key, datum);
+
+	/* Attempt to allocate a bigger table */
+	newtbl = (avtab_ptr_t*) calloc(sizeof(avtab_ptr_t), h->nslot * 2);

Order of arguments to calloc() is reversed - this is an array with h->nslot*2 members of size sizeof(avtab_ptr_t) bytes each, not the other way around. Yields the same effect but should be fixed.

Quoted 5 lines
libsepol/src/avtab.c +5 -0
+	if (newtbl == NULL)
+		return avtab_insert__(h, key, datum);
+
+	/* Reinsert all elements into the new table */
+	h->nel = 0;

libsepol/src/avtab.c int avtab_insert(avtab_t * h, avtab_key_t * key, avtab_datum_t * datum) · line 140

Quoted 10 lines
libsepol/src/avtab.c +10 -0
+	oldsize = h->nslot;
+	h->nslot *= 2;
+	h->mask = h->nslot - 1;
+	oldtbl = h->htable;
+	h->htable = newtbl;
+
+	for (i = 0; i < oldsize; i++) {
+		cur = oldtbl[i];
+		while (cur != NULL) {
+			avtab_insert(h, &(cur->key), &(cur->datum));

avtab_insert() can fail, so at the least, you need to check and handle errors, particularly ENOMEM, or you can lose data here. Also, I think you want to call __avtab_insert() rather than recursively calling avtab_insert() here, as you shouldn't need to check for resizing the table here.
Last, we could avoid both the possibility of memory allocation failure and be more efficient by reusing the already existing avtab nodes and merely re-chaining them into the new hash table. This would however require writing a bit of custom code for doing that (or factoring out a common helper) rather than just reusing __avtab_insert().

libsepol/src/hashtab.c hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, · line 66

Quoted 9 lines
libsepol/src/hashtab.c +1 -0
index c4be72c..943a2cd 100644
@@ -63,41 +63,76 @@ hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h,
 	return p;
 }
 
+int hashtab_insert__(hashtab_t h, hashtab_key_t key, hashtab_datum_t datum)

As above, this should be made static, and either use leading underscores or a meaningful suffix.

libsepol/src/hashtab.c hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, · line 108

Quoted 2 lines
libsepol/src/hashtab.c +2 -0
+	/* If we don't need to resize, just insert the new entry */
+	if (h->size > h->nel)

resize threshold should be adjustable, at least via a #define, and can be precomputed during hashtab_create().

libsepol/src/hashtab.c hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, · line 112

Quoted 4 lines
libsepol/src/hashtab.c +4 -0
+		return hashtab_insert__(h, key, datum);
+
+	/* Attempt to allocate a bigger table */
+	newtbl = (hashtab_ptr_t*) calloc(sizeof(hashtab_ptr_t), h->size * 8);

calloc order of arguments is wrong again.

libsepol/src/hashtab.c hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, · line 120

Quoted 8 lines
libsepol/src/hashtab.c +8 -0
+	if (newtbl == NULL)
+		return hashtab_insert__(h, key, datum);
+
+	/* Reinsert all elements into the new table */
+	oldtbl = h->htable;
+	oldsize = h->size;
+	h->nel = 0;
+	h->size *= 8;

The factor should likely be definable. Why 8 here vs 2 in the avtab case?

Quoted 16 lines

libsepol/src/hashtab.c hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, · line 127

Quoted 7 lines
libsepol/src/hashtab.c +7 -0
+	h->htable = newtbl;
+
+	for (i = 0; i < oldsize; i++) {
+		hashtab_ptr_t prev = NULL;
+		hashtab_ptr_t cur = oldtbl[i];
+		while (cur != NULL) {
+			hashtab_insert__(h, cur->key, cur->datum);

This can fail. Same as in the avtab case. Optimal situation would be to re-chain the existing nodes into the new hashtab rather than allocating new nodes, copying the contents, and freeing the old ones.

Signature · 2 lines
Stephen Smalley
National Security Agency

Matthew Robertson
eXMeritus Software

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#3 lore raw

Question about "rootcontext=" mount option on /dev

In reply to Stephen Smalley

Hi SELinux experts,

I have run into a file context label problem on the 2.6.34 kernel. After logging in, I can see the tmpfs is mounted on /dev by /sbin/start_udev with the "rootcontext" option:

none /dev tmpfs rw,rootcontext=system_u:object_r:device_t:s0,seclabel,relatime,mode=755 0 0

However, the /sbin/start_udev passes no such rootcontext option when calling the mount command:

mount -n -o mode=0755 -t devtmpfs none "$udev_root" 2>/dev/null \
|| mount -n -o mode=0755 -t tmpfs none "$udev_root" if [ -n "$selinuxfs" -a -x /sbin/restorecon ]; then
/sbin/restorecon -R /dev
fi

Which resulting in some device nodes in /dev/ maybe inappropriately labeled, for example, the /dev/null will be labeled as "device_t" rather than "null_device_t", and I will run into below error messages after logging in:
...
-sh: /dev/null: Permission denied
-sh: /dev/null: Permission denied
root@localhost:/root> dmesg | grep avc
...
type=1400 audit(1280386917.802:171): avc: denied { write } for pid=932 comm="sh" name="null" dev=tmpfs ino=5012 scontext=root:sysadm_r:sysadm_t:s0-s15:c0.c255 tcontext=system_u:object_r:device_t:s0
...

I guess the rootcontext=xxx:device_t option will block the tmpfs mounted on /dev/ to be properly labeled, am I right? Is the SELinux kernel driver appends such option by default? (since I couldn't find such mount option in any of system initscripts) and how could I stop it being used? BTW, such problem doesn't exist on 2.6.27 kernel, the tmpfs will be mounted onto /dev without the rootcontext option so that the successive call of " /sbin/restorecon -R /dev" could fix /dev/* labels properly.

Any comment is greatly appreciated.

Thanks,
Harry

#4 lore raw

Re: Question about "rootcontext=" mount option on /dev

Quoted 43 lines · TaurusHarry

Hi SELinux experts,

I have run into a file context label problem on the 2.6.34 kernel. After logging in, I can see the tmpfs is mounted on /dev by /sbin/start_udev with the "rootcontext" option:

none /dev tmpfs rw,rootcontext=system_u:object_r:device_t:s0,seclabel,relatime,mode=755 0 0

However, the /sbin/start_udev passes no such rootcontext option when calling the mount command:

mount -n -o mode=0755 -t devtmpfs none "$udev_root"
2>/dev/null \
|| mount -n -o mode=0755 -t tmpfs none "$udev_root" if [ -n "$selinuxfs" -a -x /sbin/restorecon ]; then
/sbin/restorecon -R /dev
fi

Which resulting in some device nodes in /dev/ maybe inappropriately labeled, for example, the /! dev/null will be labeled as "device_t" rather than "null_device_t", and I will run into below error messages after logging in:
...

(quoted diff) +0 -2
-sh: /dev/null: Permission denied
-sh: /dev/null: Permission denied

root@localhost:/root> dmesg | grep avc
...
type=1400 audit(1280386917.802:171): avc: denied { write } for pid=932 comm="sh" name="null" dev=tmpfs ino=5012 scontext=root:sysadm_r:sysadm_t:s0-s15:c0.c255 tcontext=system_u:object_r:device_t:s0
...

I guess the rootcontext=xxx:device_t option will block the tmpfs mounted on /dev/ to be properly labeled, am I right? Is the SELinux kernel driver appends such option by default? (since I couldn't find such mount option in any of system initscripts) and how could I stop it being used? BTW, such problem doesn't exist on 2.6.27 kernel, the tmpfs will be mounted onto /dev without the rootcontext option so that the successive call of " /sbin/restorecon -R /dev" could fix /dev/* labels properly.!

Quoted 1 line

Any comment is greatly appreciated.

The rootcontext= mount option is added by /sbin/mount.tmpfs; see
https://bugzilla.redhat.com/show_bug.cgi?id=476964

That ensures that the root directory of the tmpfs mount is labeled consistently with wherever it is mounted. It does not prevent restorecon from working. So something else is preventing the restorecon from executing or succeeding.

Are you sure this worked on 2.6.27 with a tmpfs mount, and not with a devtmpfs mount?

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#5 lore raw

RE: Question about "rootcontext=" mount option on /dev

Hi Stephen,

Quoted 55 lines

Subject: Re: Question about "rootcontext=" mount option on /dev
From: [email protected]
To: [email protected]
CC: [email protected]
Date: Tue, 3 Aug 2010 08:47:48 -0400

Quoted 43 lines · TaurusHarry

Hi SELinux experts,

I have run into a file context label problem on the 2.6.34 kernel. After logging in, I can see the tmpfs is mounted on /dev by /sbin/start_udev with the "rootcontext" option:

none /dev tmpfs rw,rootcontext=system_u:object_r:device_t:s0,seclabel,relatime,mode=755 0 0

However, the /sbin/start_udev passes no such rootcontext option when calling the mount command:

mount -n -o mode=0755 -t devtmpfs none "$udev_root"
2>/dev/null \
|| mount -n -o mode=0755 -t tmpfs none "$udev_root" if [ -n "$selinuxfs" -a -x /sbin/restorecon ]; then
/sbin/restorecon -R /dev
fi

Which resulting in some device nodes in /dev/ maybe inappropriately labeled, for example, the /! dev/null will be labeled as "device_t" rather than "null_device_t", and I will run into below error messages after logging in:
...

(quoted diff) +0 -2
-sh: /dev/null: Permission denied
-sh: /dev/null: Permission denied

root@localhost:/root> dmesg | grep avc
...
type=1400 audit(1280386917.802:171): avc: denied { write } for pid=932 comm="sh" name="null" dev=tmpfs ino=5012 scontext=root:sysadm_r:sysadm_t:s0-s15:c0.c255 tcontext=system_u:object_r:device_t:s0
...

I guess the rootcontext=xxx:device_t option will block the tmpfs mounted on /dev/ to be properly labeled, am I right? Is the SELinux kernel driver appends such option by default? (since I couldn't find such mount option in any of system initscripts) and how could I stop it being used? BTW, such problem doesn't exist on 2.6.27 kernel, the tmpfs will be mounted onto /dev without the rootcontext option so that the successive call of " /sbin/restorecon -R /dev" could fix /dev/* labels properly.!

Quoted 1 line

Any comment is greatly appreciated.

The rootcontext= mount option is added by /sbin/mount.tmpfs; see
https://bugzilla.redhat.com/show_bug.cgi?id=476964

Quoted 5 lines

That ensures that the root directory of the tmpfs mount is labeled consistently with wherever it is mounted. It does not prevent restorecon from working. So something else is preventing the restorecon from executing or succeeding.

Yes, you are right, even if I removed the rootcontext= option from /sbin/mount.tmpfs, the /dev/null won't be properly labeled as null_device_t but still as device_t. So the problem is not in mount.tmpfs.

Quoted 3 lines

Are you sure this worked on 2.6.27 with a tmpfs mount, and not with a devtmpfs mount?

Yes, I am using tmpfs, not devtmpfs.

I have put some debugging sentences in start_udev script, around the code segment to mount tmpfs onto /dev, before tmpfs is mounted, the original native device node of /dev/console and /dev/null have the right labels:

crw-rw---- root root system_u:object_r:console_device_t:s0 /dev/console crw-rw-rw- root root system_u:object_r:null_device_t:s0 /dev/null

And after tmpfs is mounted, the original device nodes under /dev would be disappeared and have to be re-created by udev, however, it seems to fail to label the /dev/null properly:(below are their labels I see after logging in)

sh-3.2# ls -Z /dev/null /dev/console
crw------- root root system_u:object_r:console_device_t:s0 /dev/console
crw-rw-rw- root root system_u:object_r:device_t:s0 /dev/null

I will go study carefully how the /dev/console and /dev/null are re-created and how the former gets the right label but the latter doesn't.

Best regards,
Harry

Quoted 9 lines
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.