Skip to contentSkip to conversation

wrong magic number (using old sources)

PATCH In review [email protected] 3 participants 19 messages Aug 10, 2007 – Aug 23, 2007 SELinux security module

lore
Share & tools
lore
#0 lore raw

Hi everyone,

i try to integrate SELinux into my Nokia 770 Internet Tablet. For this I'm using some old sources which fit best to the architecture (no compile errors or missing components). My current state is, that everything seems to work fine. "sestatus" tells me selinux is enabled.

But my problem is to compile a policy without error messages. It tells me, that loading fails because of a wrong magic number and I don't know where this error comes from. http://www.nsa.gov/selinux/list-archive/0206/2155.cfm tells me that it "Looks like your kernel doesn't have the same policy version as the database." I recompiled the kernel after making and make install the libraries... so I don't understand my error. A full log of the error is provided at the end of my mail.

What can be responsible for a wrong magic number? The python wrapper classes? The libraries? The policy source files i try to compile?

Thanks in advance for any help.

I'm using the following versions from the debian servers (original source packages, debian changes not included):

 Apr 10  2006 checkpolicy_1.30.orig.tar.gz
 Mar 23  2006 libselinux_1.30.orig.tar.gz
 Apr 10  2006 libsemanage_1.6.orig.tar.gz
 Mar 22  2006 libsepol_1.12.orig.tar.gz
 Apr 10  2006 policycoreutils_1.30.orig.tar.gz

Don't fit these versions?

The error log:
/usr/share/selinux/policy/beta1.0# make load
Makefile:154: warning: overriding commands for target `/etc/selinux/./policy/policy.20'
Makefile:149: warning: ignoring old commands for target `/etc/selinux/./policy/policy.20'
( cd domains/program/ ; for n in *.te ; do echo "define(\`$n')"; done ) > tmp/program_used_flags.te.tmp
( cd domains/misc/ ; for n in *.te ; do echo "define(\`$n')"; done )

Quoted 1 line
Quoted 1 line

tmp/program_used_flags.te.tmp

mv tmp/program_used_flags.te.tmp tmp/program_used_flags.te Building policy.conf ...
m4 -Imacros -s flask/security_classes [... shortened by me ...]

Quoted 1 line

policy.conf.tmp

Compiling policy ...
/usr/bin/checkpolicy -o /etc/selinux/./policy/policy.20 policy.conf

/usr/bin/checkpolicy:  loading policy configuration from policy.conf
/usr/bin/checkpolicy:  policy configuration loaded
/usr/bin/checkpolicy:  writing binary representation (version 20) to

/etc/selinux/./policy/policy.20
Loading Policy ...
/usr/sbin/load_policy /etc/selinux/./policy/policy.20 /usr/sbin/load_policy: Warning! Policy file argument (/etc/selinux/./policy/policy.20) is no longer supported, installed policy is always loaded. Continuing... libsepol.policydb_read: policydb magic number 0x8c007cff does not match expected magic number 0xf97cff8c or 0xf97cff8d libsepol.policydb_from_image: policy image is invalid libsepol.policydb_read: policydb magic number 0x8c8c7cff does not match expected magic number 0xf97cff8c or 0xf97cff8d libsepol.policydb_from_image: policy image is invalid
touch tmp/load
Building file contexts files...
Validating file contexts files ...
/usr/sbin/setfiles -q -c /etc/selinux/./policy/policy.20 file_contexts/file_contexts
libsepol.policydb_read: policydb magic number 0x8c007cff does not match expected magic number 0xf97cff8c or 0xf97cff8d libsepol.sepol_set_policydb_from_file: can't read binary policy: Success Error reading policy /etc/selinux/./policy/policy.20: Success
make: *** [tmp/valid_fc] Error 1

Signature · 7 lines
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail

--
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 6 lines · On Fri, 2007-08-10 at 12:10 +0200, [email protected] wrote:

Hi everyone,

i try to integrate SELinux into my Nokia 770 Internet Tablet. For this I'm using some old sources which fit best to the architecture (no compile errors or missing components). My current state is, that everything seems to work fine. "sestatus" tells me selinux is enabled.

You should be able to use the latest selinux userland even if using an older kernel; newer userlands can still generate older kernel policy formats.

Quoted 8 lines

But my problem is to compile a policy without error messages. It tells me, that loading fails because of a wrong magic number and I don't know where this error comes from.
http://www.nsa.gov/selinux/list-archive/0206/2155.cfm tells me that it "Looks like your kernel doesn't have the same policy version as the database." I recompiled the kernel after making and make install the libraries... so I don't understand my error. A full log of the error is provided at the end of my mail.

Looks like the policy image is corrupted not just inconsistent with kernel.

Quoted 4 lines

What can be responsible for a wrong magic number? The python wrapper classes? The libraries? The policy source files i try to compile?

Thanks in advance for any help.

Quoted 8 lines

I'm using the following versions from the debian servers (original source packages, debian changes not included):

 Apr 10  2006 checkpolicy_1.30.orig.tar.gz
 Mar 23  2006 libselinux_1.30.orig.tar.gz
 Apr 10  2006 libsemanage_1.6.orig.tar.gz
 Mar 22  2006 libsepol_1.12.orig.tar.gz
 Apr 10  2006 policycoreutils_1.30.orig.tar.gz

Don't fit these versions?

Those versions are consistent with one another.
Kernel version? Policy version?

Quoted 43 lines

The error log:
/usr/share/selinux/policy/beta1.0# make load
Makefile:154: warning: overriding commands for target `/etc/selinux/./policy/policy.20'
Makefile:149: warning: ignoring old commands for target `/etc/selinux/./policy/policy.20'
( cd domains/program/ ; for n in *.te ; do echo "define(\`$n')"; done ) > tmp/program_used_flags.te.tmp
( cd domains/misc/ ; for n in *.te ; do echo "define(\`$n')"; done )

Quoted 1 line
Quoted 1 line

tmp/program_used_flags.te.tmp

mv tmp/program_used_flags.te.tmp tmp/program_used_flags.te Building policy.conf ...
m4 -Imacros -s flask/security_classes [... shortened by me ...]

Quoted 1 line

policy.conf.tmp

Compiling policy ...
/usr/bin/checkpolicy -o /etc/selinux/./policy/policy.20 policy.conf

/usr/bin/checkpolicy:  loading policy configuration from policy.conf
/usr/bin/checkpolicy:  policy configuration loaded
/usr/bin/checkpolicy:  writing binary representation (version 20) to

/etc/selinux/./policy/policy.20
Loading Policy ...
/usr/sbin/load_policy /etc/selinux/./policy/policy.20 /usr/sbin/load_policy: Warning! Policy file argument (/etc/selinux/./policy/policy.20) is no longer supported, installed policy is always loaded. Continuing... libsepol.policydb_read: policydb magic number 0x8c007cff does not match expected magic number 0xf97cff8c or 0xf97cff8d libsepol.policydb_from_image: policy image is invalid libsepol.policydb_read: policydb magic number 0x8c8c7cff does not match expected magic number 0xf97cff8c or 0xf97cff8d libsepol.policydb_from_image: policy image is invalid
touch tmp/load
Building file contexts files...
Validating file contexts files ...
/usr/sbin/setfiles -q -c /etc/selinux/./policy/policy.20 file_contexts/file_contexts
libsepol.policydb_read: policydb magic number 0x8c007cff does not match expected magic number 0xf97cff8c or 0xf97cff8d libsepol.sepol_set_policydb_from_file: can't read binary policy: Success Error reading policy /etc/selinux/./policy/policy.20: Success
make: *** [tmp/valid_fc] Error 1

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
Quoted 35 lines · Stephen Smalley
Quoted 6 lines · On Fri, 2007-08-10 at 12:10 +0200, [email protected] wrote:

Hi everyone,

i try to integrate SELinux into my Nokia 770 Internet Tablet. For this I'm using some old sources which fit best to the architecture (no compile errors or missing components). My current state is, that everything seems to work fine. "sestatus" tells me selinux is enabled.

You should be able to use the latest selinux userland even if using an older kernel; newer userlands can still generate older kernel policy formats.

Quoted 8 lines

But my problem is to compile a policy without error messages. It tells me, that loading fails because of a wrong magic number and I don't know where this error comes from.
http://www.nsa.gov/selinux/list-archive/0206/2155.cfm tells me that it "Looks like your kernel doesn't have the same policy version as the database." I recompiled the kernel after making and make install the libraries... so I don't understand my error. A full log of the error is provided at the end of my mail.

Looks like the policy image is corrupted not just inconsistent with kernel.

Quoted 11 lines

What can be responsible for a wrong magic number? The python wrapper classes? The libraries? The policy source files i try to compile?

Thanks in advance for any help.

I'm using the following versions from the debian servers (original source packages, debian changes not included):

 Apr 10  2006 checkpolicy_1.30.orig.tar.gz
 Mar 23  2006 libselinux_1.30.orig.tar.gz
 Apr 10  2006 libsemanage_1.6.orig.tar.gz
 Mar 22  2006 libsepol_1.12.orig.tar.gz
 Apr 10  2006 policycoreutils_1.30.orig.tar.gz
Quoted 3 lines
Quoted 1 line

Don't fit these versions?

Those versions are consistent with one another.

Actually, the dates are curious - the originals were all released Mar 15 2006. Makes me wonder if those aren't pristine tarballs. md5sum of the originals was:

c67ae4e66b48f9d309467185eaf0b4b1  checkpolicy-1.30.tgz
0b7d269c9b7d847059e4b11a710ab404  libselinux-1.30.tgz
fb06d32b305322c8810dfe1924705e74  libsemanage-1.6.tgz
937885f1fcbfe597a0f02aa9af044710  libsepol-1.12.tgz
02a05b3d24483e492bace1a219425567  policycoreutils-1.30.tgz

Anyway, I'd try using the latest instead.

Quoted 45 lines

Kernel version? Policy version?

Quoted 43 lines

The error log:
/usr/share/selinux/policy/beta1.0# make load
Makefile:154: warning: overriding commands for target `/etc/selinux/./policy/policy.20'
Makefile:149: warning: ignoring old commands for target `/etc/selinux/./policy/policy.20'
( cd domains/program/ ; for n in *.te ; do echo "define(\`$n')"; done ) > tmp/program_used_flags.te.tmp
( cd domains/misc/ ; for n in *.te ; do echo "define(\`$n')"; done )

Quoted 1 line
Quoted 1 line

tmp/program_used_flags.te.tmp

mv tmp/program_used_flags.te.tmp tmp/program_used_flags.te Building policy.conf ...
m4 -Imacros -s flask/security_classes [... shortened by me ...]

Quoted 1 line

policy.conf.tmp

Compiling policy ...
/usr/bin/checkpolicy -o /etc/selinux/./policy/policy.20 policy.conf

/usr/bin/checkpolicy:  loading policy configuration from policy.conf
/usr/bin/checkpolicy:  policy configuration loaded
/usr/bin/checkpolicy:  writing binary representation (version 20) to

/etc/selinux/./policy/policy.20
Loading Policy ...
/usr/sbin/load_policy /etc/selinux/./policy/policy.20 /usr/sbin/load_policy: Warning! Policy file argument (/etc/selinux/./policy/policy.20) is no longer supported, installed policy is always loaded. Continuing... libsepol.policydb_read: policydb magic number 0x8c007cff does not match expected magic number 0xf97cff8c or 0xf97cff8d libsepol.policydb_from_image: policy image is invalid libsepol.policydb_read: policydb magic number 0x8c8c7cff does not match expected magic number 0xf97cff8c or 0xf97cff8d libsepol.policydb_from_image: policy image is invalid
touch tmp/load
Building file contexts files...
Validating file contexts files ...
/usr/sbin/setfiles -q -c /etc/selinux/./policy/policy.20 file_contexts/file_contexts
libsepol.policydb_read: policydb magic number 0x8c007cff does not match expected magic number 0xf97cff8c or 0xf97cff8d libsepol.sepol_set_policydb_from_file: can't read binary policy: Success Error reading policy /etc/selinux/./policy/policy.20: Success
make: *** [tmp/valid_fc] Error 1

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.
#3 lore raw
In reply to Stephen Smalley
Quoted 3 lines

You should be able to use the latest selinux userland even if using an older kernel; newer userlands can still generate older kernel policy formats.

The current stable userlands don't compile in my environment.
First, libselinux-1.34.7 throws a compiler error which seems to be caused by a gcc bug:
cc -shared -o libselinux.so.1 avc.lo avc_internal.lo avc_sidtab.lo booleans.lo canonicalize_context.lo checkAccess.lo check_context.lo compute_av.lo compute_cr eate.lo compute_member.lo compute_relabel.lo compute_user.lo context.lo disable. lo enabled.lo fgetfilecon.lo freecon.lo freeconary.lo fsetfilecon.lo get_context _list.lo get_default_type.lo getenforce.lo getfilecon.lo getpeercon.lo init.lo i s_customizable_type.lo lgetfilecon.lo load_policy.lo lsetfilecon.lo matchmediaco n.lo matchpathcon.lo policyvers.lo procattr.lo query_user_context.lo rpm.lo seli nux_check_securetty_context.lo selinux_config.lo setenforce.lo setfilecon.lo set rans_client.lo seusers.lo -ldl -lsepol -L/usr/lib -Wl,-soname,libselinux.so.1,-z ,defs,-z,relro matchpathcon.lo: In function set_matchpathcon_flags':matchpathcon.c:(.text+0x27 0): undefined reference to __tls_get_addr'
matchpathcon.lo: In function process_line':matchpathcon.c:(.text+0x1cf0): undef ined reference to __tls_get_addr'
matchpathcon.lo: In function matchpathcon_init_prefix':matchpathcon.c:(.text+0x 1fdc): undefined reference to __tls_get_addr'
matchpathcon.lo: In function matchpathcon':matchpathcon.c:(.text+0x2eb0): undef ined reference to __tls_get_addr'
matchpathcon.lo: In function selinux_file_context_verify':matchpathcon.c:(.text +0x3400): undefined reference to __tls_get_addr'
matchpathcon.lo:matchpathcon.c:(.text+0x3468): more undefined references to `__t ls_get_addr' follow
collect2: ld returned 1 exit status
make[1]: *** [libselinux.so.1] Error 1

It could be solved by removing -z,defs from the src/Makefile. I don't know the consequences but it works.

Second, the policycoreutils-1.34-6 won't compile because of a missing header file, which is not existent in my environment:
cc -g -Werror -Wall -W -I/usr/include -D_FILE_OFFSET_BITS=64 -c -o restorecond.o restorecond.c
restorecond.c:44:25: sys/inotify.h: No such file or directory
restorecond.c: In function `watch_list_free':
restorecond.c:144: warning: implicit declaration of function `inotify_rm_watch'
restorecond.c: In function `read_config':
restorecond.c:283: warning: implicit declaration of function `inotify_add_watch'
restorecond.c:283: error: `IN_MOVED_FROM' undeclared (first use in this function)
restorecond.c:283: error: (Each undeclared identifier is reported only once
restorecond.c:283: error: for each function it appears in.)
restorecond.c:283: error: `IN_MODIFY' undeclared (first use in this function)
restorecond.c: In function `watch':
restorecond.c:291: error: invalid application of sizeof' to incomplete type inotify_event' restorecond.c:293: error: invalid application of sizeof' to incomplete type inotify_event' restorecond.c:309: error: dereferencing pointer to incomplete type
restorecond.c:309: error: dereferencing pointer to incomplete type
restorecond.c:310: error: dereferencing pointer to incomplete type
restorecond.c:310: error: dereferencing pointer to incomplete type
restorecond.c:311: error: dereferencing pointer to incomplete type
restorecond.c:314: error: dereferencing pointer to incomplete type
restorecond.c:316: error: dereferencing pointer to incomplete type
restorecond.c:317: error: dereferencing pointer to incomplete type
restorecond.c:317: error: dereferencing pointer to incomplete type
restorecond.c:329: error: invalid application of sizeof' to incomplete type inotify_event' restorecond.c:329: error: dereferencing pointer to incomplete type
restorecond.c: In function `watch_list_add':
restorecond.c:412: error: `IN_CREATE' undeclared (first use in this function)
restorecond.c:412: error: `IN_MOVED_TO' undeclared (first use in this function)
restorecond.c: In function `main':
restorecond.c:456: warning: implicit declaration of function `inotify_init'
restorecond.c:484: warning: implicit declaration of function `matchpathcon_fini'
make[1]: *** [restorecond.o] Error 1

That's why I'm using the old sources. They work.

Quoted 2 lines

Those versions are consistent with one another.
Kernel version? Policy version?

kernel version is 2.6.16-omap1
How do I find out the policy version?

Signature · 8 lines
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger


--
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.
#4 lore raw
Quoted 16 lines · On Fri, 2007-08-10 at 14:12 +0200, [email protected] wrote:
Quoted 3 lines

You should be able to use the latest selinux userland even if using an older kernel; newer userlands can still generate older kernel policy formats.

The current stable userlands don't compile in my environment.
First, libselinux-1.34.7 throws a compiler error which seems to be caused by a gcc bug:
cc -shared -o libselinux.so.1 avc.lo avc_internal.lo avc_sidtab.lo booleans.lo canonicalize_context.lo checkAccess.lo check_context.lo compute_av.lo compute_cr eate.lo compute_member.lo compute_relabel.lo compute_user.lo context.lo disable. lo enabled.lo fgetfilecon.lo freecon.lo freeconary.lo fsetfilecon.lo get_context _list.lo get_default_type.lo getenforce.lo getfilecon.lo getpeercon.lo init.lo i s_customizable_type.lo lgetfilecon.lo load_policy.lo lsetfilecon.lo matchmediaco n.lo matchpathcon.lo policyvers.lo procattr.lo query_user_context.lo rpm.lo seli nux_check_securetty_context.lo selinux_config.lo setenforce.lo setfilecon.lo set rans_client.lo seusers.lo -ldl -lsepol -L/usr/lib -Wl,-soname,libselinux.so.1,-z ,defs,-z,relro matchpathcon.lo: In function set_matchpathcon_flags':matchpathcon.c:(.text+0x27 0): undefined reference to __tls_get_addr'
matchpathcon.lo: In function process_line':matchpathcon.c:(.text+0x1cf0): undef ined reference to __tls_get_addr'
matchpathcon.lo: In function matchpathcon_init_prefix':matchpathcon.c:(.text+0x 1fdc): undefined reference to __tls_get_addr'
matchpathcon.lo: In function matchpathcon':matchpathcon.c:(.text+0x2eb0): undef ined reference to __tls_get_addr'
matchpathcon.lo: In function selinux_file_context_verify':matchpathcon.c:(.text +0x3400): undefined reference to __tls_get_addr'
matchpathcon.lo:matchpathcon.c:(.text+0x3468): more undefined references to `__t ls_get_addr' follow
collect2: ld returned 1 exit status
make[1]: *** [libselinux.so.1] Error 1

Quoted 1 line

It could be solved by removing -z,defs from the src/Makefile. I don't know the consequences but it works.

No, it depends on thread local storage support in your glibc. Manoj was using a workaround patch for it at one time for Debian,
http://marc.info/?l=selinux&m=115807948020898&w=2

Quoted 3 lines

Second, the policycoreutils-1.34-6 won't compile because of a missing header file, which is not existent in my environment:
cc -g -Werror -Wall -W -I/usr/include -D_FILE_OFFSET_BITS=64 -c -o restorecond.o restorecond.c
restorecond.c:44:25: sys/inotify.h: No such file or directory

restorecond depends on inotify. But you don't need restorecond to have a working system, so you can always remove it from the Makefile SUBDIRS definition.

Quoted 1 line

That's why I'm using the old sources. They work.

Well, except that they evidently don't.

Quoted 4 lines
Quoted 2 lines

Those versions are consistent with one another.
Kernel version? Policy version?

kernel version is 2.6.16-omap1
How do I find out the policy version?

Same way you found out what package version you were using of checkpolicy and friends, I assume.

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
Quoted 10 lines
Quoted 1 line

It could be solved by removing -z,defs from the src/Makefile. I don't

know the consequences but it works.

No, it depends on thread local storage support in your glibc. Manoj was using a workaround patch for it at one time for Debian,
http://marc.info/?l=selinux&m=115807948020898&w=2

restorecond depends on inotify. But you don't need restorecond to have a working system, so you can always remove it from the Makefile SUBDIRS definition.

I'll try that. Thanks.

Quoted 3 lines
Quoted 1 line

That's why I'm using the old sources. They work.

Well, except that they evidently don't.

Ok, you're right. Actually I meant: They compile with less problems ;o)

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Those versions are consistent with one another.
Kernel version? Policy version?

kernel version is 2.6.16-omap1
How do I find out the policy version?

Same way you found out what package version you were using of checkpolicy and friends, I assume.

Then i think it's
selinux-policy-default_1.26-7 or
selinux-policy-default_1.26

I'm not really sure about that, because the policy was not written by me. It's only for testing. If the version of the policy is relevant, this might by the most probable error. Which version has the policyto be (at least) to work with the (old) packages I mentioned?

Signature · 7 lines
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

--
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.
#6 lore raw
Quoted 24 lines · Björn Vogel
Quoted 10 lines
Quoted 1 line

It could be solved by removing -z,defs from the src/Makefile. I don't

know the consequences but it works.

No, it depends on thread local storage support in your glibc. Manoj was using a workaround patch for it at one time for Debian,
http://marc.info/?l=selinux&m=115807948020898&w=2

restorecond depends on inotify. But you don't need restorecond to have a working system, so you can always remove it from the Makefile SUBDIRS definition.

I'll try that. Thanks.

Quoted 3 lines
Quoted 1 line

That's why I'm using the old sources. They work.

Well, except that they evidently don't.

Ok, you're right. Actually I meant: They compile with less problems ;o)

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

Those versions are consistent with one another.
Kernel version? Policy version?

kernel version is 2.6.16-omap1
How do I find out the policy version?

Quoted 8 lines
Quoted 2 lines

Same way you found out what package version you were using of checkpolicy and friends, I assume.

Then i think it's
selinux-policy-default_1.26-7 or
selinux-policy-default_1.26

I'm not really sure about that, because the policy was not written by me. It's only for testing. If the version of the policy is relevant, this might by the most probable error. Which version has the policyto be (at least) to work with the (old) packages I mentioned?

That should be fine; you'd only run into problems if using newer reference policy versions with that older checkpolicy.

I'd suggest sending me a copy of your policy.conf and policy.20 off-list, and ideally giving me the precise URLs where I can find the .tar.gz files that you used as your baseline. Then I can try to re-create the bug.

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.
#7 lore raw
Quoted 9 lines
Quoted 5 lines

Then i think it's
selinux-policy-default_1.26-7 or
selinux-policy-default_1.26

I'm not really sure about that, because the policy was not written by

me. It's only for testing. If the version of the policy is relevant, this might by the most probable error. Which version has the policyto be (at least)
to work with the (old) packages I mentioned?

Quoted 7 lines

That should be fine; you'd only run into problems if using newer reference policy versions with that older checkpolicy.

I'd suggest sending me a copy of your policy.conf and policy.20 off-list, and ideally giving me the precise URLs where I can find the .tar.gz files that you used as your baseline. Then I can try to re-create the bug.

Ok, I just tried to compile the up-to-date stable package from the nsa page. Compiling seems to work with the help you provided. Thanks again for that.

I will now try to bring the system up now with the new packages. This might take some days. I'll notify you about results.

Signature · 7 lines
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

--
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.
#8 lore raw

Hello again,

unfortunately, the problem persists. I've compiled the current stable package and put the files on the target syste... the error is still "wrong magic number"... I also recompiled the kernel... btw... is this necessary each time i change the SELinux libraries versions? I did that too many times the last week ;-)

I've got to confess it was just a quick shot, maybe i forgot something, but I don't think so. I'll retry it on monday once a again, but I'm not very confident.

Is there some non-SELinux related library that may be responsible for this error? How is the magic number generated? How is it checked? Is some calculation or transformation performed on this number by other libraries? Since I cross-compile all the packages it may be the case that some libraries differ on the compiling system and the using system. It would be helpful to have a list to check these packages/libraries against inconsistencies that may be responsible for that....

-------- Original-Nachricht --------
Datum: Fri, 10 Aug 2007 16:12:36 +0200
Von: [email protected]
An: [email protected]
CC: Stephen Smalley <[email protected]>
Betreff: Re: wrong magic number (using old sources)

Quoted 34 lines
Quoted 6 lines
Quoted 5 lines

Then i think it's
selinux-policy-default_1.26-7 or
selinux-policy-default_1.26

I'm not really sure about that, because the policy was not written by

me. It's only for testing. If the version of the policy is relevant,

this

Quoted 1 line

might by the most probable error. Which version has the policyto be (at

least)

Quoted 9 lines

to work with the (old) packages I mentioned?

That should be fine; you'd only run into problems if using newer reference policy versions with that older checkpolicy.

I'd suggest sending me a copy of your policy.conf and policy.20 off-list, and ideally giving me the precise URLs where I can find the .tar.gz files that you used as your baseline. Then I can try to re-create the bug.

Ok, I just tried to compile the up-to-date stable package from the nsa page. Compiling seems to work with the help you provided. Thanks again for that.

I will now try to bring the system up now with the new packages. This might take some days. I'll notify you about results.

Signature · 8 lines
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

--
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.
Signature · 7 lines
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

--
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.
#9 lore raw
Quoted 8 lines · On Sat, 2007-08-11 at 20:07 +0200, [email protected] wrote:

Hello again,

unfortunately, the problem persists. I've compiled the current stable package and put the files on the target syste... the error is still "wrong magic number"...
I also recompiled the kernel... btw... is this necessary each time i change the SELinux libraries versions? I did that too many times the last week ;-)

No, you don't have to do that.

Quoted 4 lines

I've got to confess it was just a quick shot, maybe i forgot something, but I don't think so. I'll retry it on monday once a again, but I'm not very confident.

Quoted 8 lines

Is there some non-SELinux related library that may be responsible for this error? How is the magic number generated? How is it checked? Is some calculation or transformation performed on this number by other libraries? Since I cross-compile all the packages it may be the case that some libraries differ on the compiling system and the using system. It would be helpful to have a list to check these packages/libraries against inconsistencies that may be responsible for that....

libsepol/src/write.c:policydb_write() writes the policy image from memory to disk. If you look at it, you'll see that it puts the magic number (just a fixed value, defined as POLICYDB_MAGIC) and the length of a string identifier into a buffer (converting both to little endian order), then puts them to the file via put_entry().

libsepol/src/policydb.c:policydb_read() reads the policy image from disk into memory. If you look at it, you'll see that it fetches the first two words via next_entry(), converts them from little endian to cpu order, and then checks the magic number to see whether it is a kernel policy or a policy module, displaying the error message you are seeing if it doesn't match either.

Are you cross-compiling from a system with different endianness?

As I've suggested before, you could send your policy.conf and policy.20 files to me separately and I could see whether I can reproduce here.

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.
#10 lore raw
Quoted 1 line

Are you cross-compiling from a system with different endianness?

Don't think so. I think both systems are little endian.

I played a little bit with the sources and i found a strange error:
Actuallay, the generated magic number is correct. I checked that with an hex editor and also within the next_entry() method in private.h the buffer, the next_entry method returns is:
buffer[0]: 8c
buffer[1]: ff
buffer[2]: 7c
buffer[3]: f9
buffer[4]: 8
buffer[5]: 0
buffer[6]: 0
buffer[7]: 0

The method returns a void pointer, that is interpreted as an uint32_t (4 bytes) pointer (*buf). Printing the first two buffer entries now, i get:
*** before conversion: buf[0] : 7cff8c
*** after conversion: buf[0] : 8c007cff
*** before conversion: buf[1] : f9000008
*** after conversion: buf[1] : 8f90000

Whereas "before conversion" shows the hex interpretation of buf[i] before le32_to_cpu(buf[i]) is called, and "after conversion" shows the value afterwards. Something wents completely wrong. It seems like a 00 is added at the beginning of buf[0]. The magic number would be correct if we would drop this 00 and take the first byte of buf[1] instead. The strange thing is that, when i declare a
char *tmpbuf;
and set it to
tmpbuf=(char *)buf;
the content is correct again:

***  buf as char: tmpbuf[0] : 8c
***  buf as char: tmpbuf[1] : ff
***  buf as char: tmpbuf[2] : 7c
***  buf as char: tmpbuf[3] : f9
***  buf as char: tmpbuf[4] : 8
***  buf as char: tmpbuf[5] : 0
***  buf as char: tmpbuf[6] : 0
***  buf as char: tmpbuf[7] : 0

I don't get the problem... why is

         buf[0]     = (00) 7c ff 8c and

(char *) buf[3...0] = f9 7c ff 8c
?

This is obviously not an error within the selinux sources, but maybe you could help me anyway?!

Thanks in advance.

Signature · 8 lines
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger


--
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.
#11 lore raw
Quoted 35 lines · On Wed, 2007-08-15 at 17:44 +0200, [email protected] wrote:
Quoted 1 line

Are you cross-compiling from a system with different endianness?

Don't think so. I think both systems are little endian.

I played a little bit with the sources and i found a strange error:
Actuallay, the generated magic number is correct. I checked that with an hex editor and also within the next_entry() method in private.h the buffer, the next_entry method returns is:
buffer[0]: 8c
buffer[1]: ff
buffer[2]: 7c
buffer[3]: f9
buffer[4]: 8
buffer[5]: 0
buffer[6]: 0
buffer[7]: 0

The method returns a void pointer, that is interpreted as an uint32_t (4 bytes) pointer (*buf). Printing the first two buffer entries now, i get:
*** before conversion: buf[0] : 7cff8c
*** after conversion: buf[0] : 8c007cff
*** before conversion: buf[1] : f9000008
*** after conversion: buf[1] : 8f90000

Whereas "before conversion" shows the hex interpretation of buf[i] before le32_to_cpu(buf[i]) is called, and "after conversion" shows the value afterwards. Something wents completely wrong. It seems like a 00 is added at the beginning of buf[0]. The magic number would be correct if we would drop this 00 and take the first byte of buf[1] instead. The strange thing is that, when i declare a
char *tmpbuf;
and set it to
tmpbuf=(char *)buf;
the content is correct again:

***  buf as char: tmpbuf[0] : 8c
***  buf as char: tmpbuf[1] : ff
***  buf as char: tmpbuf[2] : 7c
***  buf as char: tmpbuf[3] : f9
***  buf as char: tmpbuf[4] : 8
***  buf as char: tmpbuf[5] : 0
***  buf as char: tmpbuf[6] : 0
***  buf as char: tmpbuf[7] : 0
Quoted 8 lines

I don't get the problem... why is

         buf[0]     = (00) 7c ff 8c and

(char *) buf[3...0] = f9 7c ff 8c
?

This is obviously not an error within the selinux sources, but maybe you could help me anyway?!

Thanks in advance.

I suspect that what we are doing isn't safe/portable.

The corresponding kernel code was converted a while back to avoid unaligned access problems, so possibly a similar transformation should happen here.

See:
http://marc.info/?l=selinux&m=110252376515271&w=2

As a simple test of whether this is related, you might try the following patch for libsepol (and then rebuild checkpolicy against the updated libsepol) and see if it gets you past the magic number check. If so, then the next step would be to apply the same change to the entire policydb_read code.

src/policydb.c +9 -8
Index: libsepol/src/policydb.c
===================================================================
@@ -2938,36 +2938,37 @@
 {
 
 	unsigned int i, j, r_policyvers;
-	uint32_t *buf, config;
+	uint32_t *buf, buf2[8], config;
 	size_t len, nprim, nel;
 	char *policydb_str, *target_str = NULL;
 	struct policydb_compat_info *info;
 	unsigned int policy_type, bufindex;
 	ebitmap_node_t *tnode;
+	int rc;
 
 	config = 0;
 
 	/* Read the magic number and string length. */
-	buf = next_entry(fp, sizeof(uint32_t) * 2);
-	if (!buf)
+	rc = next_entry2(buf2, fp, sizeof(uint32_t) * 2);
+	if (rc < 0)
 		return POLICYDB_ERROR;
 	for (i = 0; i < 2; i++)
-		buf[i] = le32_to_cpu(buf[i]);
+		buf2[i] = le32_to_cpu(buf2[i]);
 
-	if (buf[0] == POLICYDB_MAGIC) {
+	if (buf2[0] == POLICYDB_MAGIC) {
 		policy_type = POLICY_KERN;
 		target_str = POLICYDB_STRING;
-	} else if (buf[0] == POLICYDB_MOD_MAGIC) {
+	} else if (buf2[0] == POLICYDB_MOD_MAGIC) {
 		policy_type = POLICY_MOD;
 		target_str = POLICYDB_MOD_STRING;
 	} else {
 		ERR(fp->handle, "policydb magic number %#08x does not "
 		    "match expected magic number %#08x or %#08x",
-		    buf[0], POLICYDB_MAGIC, POLICYDB_MOD_MAGIC);
+		    buf2[0], POLICYDB_MAGIC, POLICYDB_MOD_MAGIC);
 		return POLICYDB_ERROR;
 	}
 
-	len = buf[1];
+	len = buf2[1];
 	if (len != strlen(target_str)) {
 		ERR(fp->handle, "policydb string length %zu does not match "
 		    "expected length %zu", len, strlen(target_str));
src/private.h +23 -0
Index: libsepol/src/private.h
===================================================================
@@ -64,6 +64,29 @@
 	return buffer;
 }
 
+static inline int next_entry2(void *buf, struct policy_file *fp, size_t bytes)
+{
+	size_t nread;
+
+	switch (fp->type) {
+	case PF_USE_STDIO:
+		nread = fread(buf, bytes, 1, fp->fp);
+		if (nread != 1)
+			return -1;
+		break;
+	case PF_USE_MEMORY:
+		if (bytes > fp->len)
+			return -1;
+		memcpy(buf, fp->data, bytes);
+		fp->data += bytes;
+		fp->len -= bytes;
+		break;
+	default:
+		return -1;
+	}
+	return 0;
+}
+
 static inline size_t put_entry(const void *ptr, size_t size, size_t n,
 			       struct policy_file *fp)
 {
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.
#12 lore raw
Quoted 6 lines

I suspect that what we are doing isn't safe/portable.

The corresponding kernel code was converted a while back to avoid unaligned access problems, so possibly a similar transformation should happen here.

Quoted 8 lines

See:
http://marc.info/?l=selinux&m=110252376515271&w=2

As a simple test of whether this is related, you might try the following patch for libsepol (and then rebuild checkpolicy against the updated libsepol) and see if it gets you past the magic number check. If so, then the next step would be to apply the same change to the entire policydb_read code.

That's it. After applying the changes in source code i get past the magic number check. Now, my policyversion number is wrong but i would suggest, that this error is related to the problem you described. I included also a printf for buf2[] to check the values after conversion:

# make
( cd domains/program/ ; for n in *.te ; do echo "define(\`$n')"; done ) > tmp/program_used_flags.te.tmp
( cd domains/misc/ ; for n in *.te ; do echo "define(\`$n')"; done ) >> tmp/program_used_flags.te.tmp
mv tmp/program_used_flags.te.tmp tmp/program_used_flags.te
Building policy.conf ...
[...] > policy.conf.tmp
Building file contexts files...
/usr/bin/checkpolicy  -o policy.20 policy.conf
/usr/bin/checkpolicy:  loading policy configuration from policy.conf
/usr/bin/checkpolicy:  policy configuration loaded
/usr/bin/checkpolicy:  writing binary representation (version 21) to policy.20
Validating file contexts files ...
/usr/sbin/setfiles -q -c policy.20 file_contexts/file_contexts
 buf2[0]: f97cff8c
 buf2[1]: 8
 buf2[2]: bea60574
 buf2[3]: bea60560
 buf2[4]: 4004a304
 buf2[5]: 400469a4
 buf2[6]: 0
 buf2[7]: 400a26f0
libsepol.policydb_read: policydb version 352321536 does not match my version range 15-21
libsepol.sepol_set_policydb_from_file: can't read binary policy: Success
Error reading policy policy.20: Success
make: *** [policy.20] Error 1

I think it is only necessary to declare unint32_t buf2[] of size 2 and not 8, isn't it?

Signature · 7 lines
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail

--
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.
#13 lore raw
Quoted 42 lines · On Thu, 2007-08-16 at 10:05 +0200, [email protected] wrote:
Quoted 6 lines

I suspect that what we are doing isn't safe/portable.

The corresponding kernel code was converted a while back to avoid unaligned access problems, so possibly a similar transformation should happen here.

Quoted 8 lines

See:
http://marc.info/?l=selinux&m=110252376515271&w=2

As a simple test of whether this is related, you might try the following patch for libsepol (and then rebuild checkpolicy against the updated libsepol) and see if it gets you past the magic number check. If so, then the next step would be to apply the same change to the entire policydb_read code.

That's it. After applying the changes in source code i get past the magic number check. Now, my policyversion number is wrong but i would suggest, that this error is related to the problem you described. I included also a printf for buf2[] to check the values after conversion:

# make
( cd domains/program/ ; for n in *.te ; do echo "define(\`$n')"; done ) > tmp/program_used_flags.te.tmp
( cd domains/misc/ ; for n in *.te ; do echo "define(\`$n')"; done ) >> tmp/program_used_flags.te.tmp
mv tmp/program_used_flags.te.tmp tmp/program_used_flags.te
Building policy.conf ...
[...] > policy.conf.tmp
Building file contexts files...
/usr/bin/checkpolicy  -o policy.20 policy.conf
/usr/bin/checkpolicy:  loading policy configuration from policy.conf
/usr/bin/checkpolicy:  policy configuration loaded
/usr/bin/checkpolicy:  writing binary representation (version 21) to policy.20
Validating file contexts files ...
/usr/sbin/setfiles -q -c policy.20 file_contexts/file_contexts
 buf2[0]: f97cff8c
 buf2[1]: 8
 buf2[2]: bea60574
 buf2[3]: bea60560
 buf2[4]: 4004a304
 buf2[5]: 400469a4
 buf2[6]: 0
 buf2[7]: 400a26f0
libsepol.policydb_read: policydb version 352321536 does not match my version range 15-21
libsepol.sepol_set_policydb_from_file: can't read binary policy: Success
Error reading policy policy.20: Success
make: *** [policy.20] Error 1
Quoted 1 line

I think it is only necessary to declare unint32_t buf2[] of size 2 and not 8, isn't it?

Ok, so libsepol needs to be fixed in the same way the kernel was to eliminate unaligned accesses. Might take a little bit to work through all the code. The size of the buffer has to be large enough for the largest next_entry() request.

As a possible short term work around, you might try this patch instead. It just converts the static buffer used within libsepol from unsigned char to uint32_t so that the buffer will be aligned (at least for u32). That avoids changing all the callers.

src/private.h +2 -1
Index: libsepol/src/private.h
===================================================================
@@ -5,6 +5,7 @@
 #include <byteswap.h>
 #include <endian.h>
 #include <sepol/policydb/policydb.h>
+#include <stdint.h>
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #define cpu_to_le16(x) (x)
@@ -39,7 +40,7 @@
 /* Reading from a policy "file". */
 static inline void *next_entry(struct policy_file *fp, size_t bytes)
 {
-	static unsigned char buffer[BUFSIZ];
+	static uint32_t buffer[20];
 	size_t nread;
 
 	if (bytes > sizeof buffer)
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.
#14 lore raw
Quoted 9 lines

Ok, so libsepol needs to be fixed in the same way the kernel was to eliminate unaligned accesses. Might take a little bit to work through all the code. The size of the buffer has to be large enough for the largest next_entry() request.

As a possible short term work around, you might try this patch instead. It just converts the static buffer used within libsepol from unsigned char to uint32_t so that the buffer will be aligned (at least for u32). That avoids changing all the callers.

This smal patch worked fine for me. The policy is created and loaded (correctly I think :-)).

Thanks a lot for you support.

Quoted 25 lines

Index: libsepol/src/private.h
===================================================================

src/private.h +2 -1
@@ -5,6 +5,7 @@
 #include <byteswap.h>
 #include <endian.h>
 #include <sepol/policydb/policydb.h>
+#include <stdint.h>
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #define cpu_to_le16(x) (x)
@@ -39,7 +40,7 @@
 /* Reading from a policy "file". */
 static inline void *next_entry(struct policy_file *fp, size_t bytes)
 {
-	static unsigned char buffer[BUFSIZ];
+	static uint32_t buffer[20];
 	size_t nread;
 
 	if (bytes > sizeof buffer)
Signature · 3 lines
-- 
Stephen Smalley
National Security Agency
Signature · 7 lines
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail

--
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.
#15 lore raw

PATCH 1/1 libsepol: eliminate unaligned accesses (Was: Re: wrong magic number (using old sources))

In reply to Stephen Smalley

Full patch follows, replaces the prior short term one.

Rewrite libsepol next_entry function and all callers to copy entry data from the binary policy into properly aligned buffers, eliminating unaligned accesses, just as I did for the kernel back in 2004,
http://marc.info/?l=selinux&m=110252376515271&w=2

---

 libsepol/src/avtab.c       |   32 +--
 libsepol/src/conditional.c |   44 ++--
 libsepol/src/ebitmap.c     |   23 +-
 libsepol/src/module.c      |  111 +++++-----
 libsepol/src/policydb.c    |  472 +++++++++++++++++++++++----------------------
 libsepol/src/private.h     |   20 -
 6 files changed, 365 insertions(+), 337 deletions(-)
libsepol/src/conditional.c +22 -22
Index: trunk/libsepol/src/conditional.c
===================================================================
@@ -569,15 +569,16 @@
 {
 	char *key = 0;
 	cond_bool_datum_t *booldatum;
-	uint32_t *buf, len;
+	uint32_t buf[3], len;
+	int rc;
 
 	booldatum = malloc(sizeof(cond_bool_datum_t));
 	if (!booldatum)
 		return -1;
 	memset(booldatum, 0, sizeof(cond_bool_datum_t));
 
-	buf = next_entry(fp, sizeof(uint32_t) * 3);
-	if (!buf)
+	rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
+	if (rc < 0)
 		goto err;
 
 	booldatum->s.value = le32_to_cpu(buf[0]);
@@ -588,13 +589,12 @@
 
 	len = le32_to_cpu(buf[2]);
 
-	buf = next_entry(fp, len);
-	if (!buf)
-		goto err;
 	key = malloc(len + 1);
 	if (!key)
 		goto err;
-	memcpy(key, buf, len);
+	rc = next_entry(key, fp, len);
+	if (rc < 0)
+		goto err;
 	key[len] = 0;
 	if (hashtab_insert(h, key, booldatum))
 		goto err;
@@ -703,14 +703,14 @@
 {
 	unsigned int i;
 	int rc;
-	uint32_t *buf, len;
+	uint32_t buf[1], len;
 	struct cond_insertf_data data;
 
 	*ret_list = NULL;
 
 	len = 0;
-	buf = next_entry(fp, sizeof(uint32_t));
-	if (!buf)
+	rc = next_entry(buf, fp, sizeof(uint32_t));
+	if (rc < 0)
 		return -1;
 
 	len = le32_to_cpu(buf[0]);
@@ -752,27 +752,27 @@
 
 static int cond_read_node(policydb_t * p, cond_node_t * node, void *fp)
 {
-	uint32_t *buf;
-	int len, i;
+	uint32_t buf[2];
+	int len, i, rc;
 	cond_expr_t *expr = NULL, *last = NULL;
 
-	buf = next_entry(fp, sizeof(uint32_t));
-	if (!buf)
+	rc = next_entry(buf, fp, sizeof(uint32_t));
+	if (rc < 0)
 		goto err;
 
 	node->cur_state = le32_to_cpu(buf[0]);
 
 	len = 0;
-	buf = next_entry(fp, sizeof(uint32_t));
-	if (!buf)
+	rc = next_entry(buf, fp, sizeof(uint32_t));
+	if (rc < 0)
 		goto err;
 
 	/* expr */
 	len = le32_to_cpu(buf[0]);
 
 	for (i = 0; i < len; i++) {
-		buf = next_entry(fp, sizeof(uint32_t) * 2);
-		if (!buf)
+		rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
+		if (rc < 0)
 			goto err;
 
 		expr = malloc(sizeof(cond_expr_t));
@@ -820,11 +820,11 @@
 int cond_read_list(policydb_t * p, cond_list_t ** list, void *fp)
 {
 	cond_node_t *node, *last = NULL;
-	uint32_t *buf;
-	int i, len;
+	uint32_t buf[1];
+	int i, len, rc;
 
-	buf = next_entry(fp, sizeof(uint32_t));
-	if (!buf)
+	rc = next_entry(buf, fp, sizeof(uint32_t));
+	if (rc < 0)
 		return -1;
 
 	len = le32_to_cpu(buf[0]);
libsepol/src/policydb.c +245 -227

1,086 lines not on this page · raw message

libsepol/src/private.h +9 -11
Index: trunk/libsepol/src/private.h
===================================================================
@@ -27,6 +27,8 @@
 #undef min
 #define min(a,b) (((a) < (b)) ? (a) : (b))
 
+#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
+
 /* Policy compatibility information. */
 struct policydb_compat_info {
 	unsigned int type;
@@ -39,31 +41,27 @@
 							   unsigned int type);
 
 /* Reading from a policy "file". */
-static inline void *next_entry(struct policy_file *fp, size_t bytes)
+static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes)
 {
-	static unsigned char buffer[BUFSIZ];
 	size_t nread;
 
-	if (bytes > sizeof buffer)
-		return NULL;
-
 	switch (fp->type) {
 	case PF_USE_STDIO:
-		nread = fread(buffer, bytes, 1, fp->fp);
+		nread = fread(buf, bytes, 1, fp->fp);
 		if (nread != 1)
-			return NULL;
+			return -1;
 		break;
 	case PF_USE_MEMORY:
 		if (bytes > fp->len)
-			return NULL;
-		memcpy(buffer, fp->data, bytes);
+			return -1;
+		memcpy(buf, fp->data, bytes);
 		fp->data += bytes;
 		fp->len -= bytes;
 		break;
 	default:
-		return NULL;
+		return -1;
 	}
-	return buffer;
+	return 0;
 }
 
 static inline size_t put_entry(const void *ptr, size_t size, size_t n,
libsepol/src/ebitmap.c +12 -11
Index: trunk/libsepol/src/ebitmap.c
===================================================================
@@ -265,16 +265,16 @@
 
 int ebitmap_read(ebitmap_t * e, void *fp)
 {
-	int rc = -EINVAL;
+	int rc;
 	ebitmap_node_t *n, *l;
-	uint32_t *buf, mapsize, count, i;
+	uint32_t buf[3], mapsize, count, i;
 	uint64_t map;
 
 	ebitmap_init(e);
 
-	buf = next_entry(fp, sizeof(uint32_t) * 3);
-	if (!buf)
-		goto out;
+	rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
+	if (rc < 0)
+		goto bad;
 
 	mapsize = le32_to_cpu(buf[0]);
 	e->highbit = le32_to_cpu(buf[1]);
@@ -284,7 +284,7 @@
 		printf
 		    ("security: ebitmap: map size %d does not match my size %zu (high bit was %d)\n",
 		     mapsize, MAPSIZE, e->highbit);
-		goto out;
+		goto bad;
 	}
 	if (!e->highbit) {
 		e->node = NULL;
@@ -298,8 +298,8 @@
 	}
 	l = NULL;
 	for (i = 0; i < count; i++) {
-		buf = next_entry(fp, sizeof(uint32_t));
-		if (!buf) {
+		rc = next_entry(buf, fp, sizeof(uint32_t));
+		if (rc < 0) {
 			printf("security: ebitmap: truncated map\n");
 			goto bad;
 		}
@@ -325,12 +325,11 @@
 			     n->startbit, (e->highbit - MAPSIZE));
 			goto bad_free;
 		}
-		buf = next_entry(fp, sizeof(uint64_t));
-		if (!buf) {
+		rc = next_entry(&map, fp, sizeof(uint64_t));
+		if (rc < 0) {
 			printf("security: ebitmap: truncated map\n");
 			goto bad_free;
 		}
-		memcpy(&map, buf, sizeof(uint64_t));
 		n->map = le64_to_cpu(map);
 
 		if (!n->map) {
@@ -360,6 +359,8 @@
       bad_free:
 	free(n);
       bad:
+	if (!rc)
+		rc = -EINVAL;
 	ebitmap_destroy(e);
 	goto out;
 }
libsepol/src/module.c +61 -50

232 lines not on this page · raw message

libsepol/src/avtab.c +16 -16
Index: trunk/libsepol/src/avtab.c
===================================================================
@@ -337,8 +337,8 @@
 		    int (*insertf) (avtab_t * a, avtab_key_t * k,
 				    avtab_datum_t * d, void *p), void *p)
 {
-	uint16_t *buf16, enabled;
-	uint32_t *buf32, items, items2, val;
+	uint16_t buf16[4], enabled;
+	uint32_t buf32[7], items, items2, val;
 	avtab_key_t key;
 	avtab_datum_t datum;
 	unsigned set;
@@ -349,20 +349,20 @@
 	memset(&datum, 0, sizeof(avtab_datum_t));
 
 	if (vers < POLICYDB_VERSION_AVTAB) {
-		buf32 = next_entry(fp, sizeof(uint32_t));
-		if (!buf32) {
+		rc = next_entry(buf32, fp, sizeof(uint32_t));
+		if (rc < 0) {
 			ERR(fp->handle, "truncated entry");
 			return -1;
 		}
 		items2 = le32_to_cpu(buf32[0]);
 
-		if (items2 < 5 || items2 > 8) {
+		if (items2 < 5 || items2 > ARRAY_SIZE(buf32)) {
 			ERR(fp->handle, "invalid item count");
 			return -1;
 		}
 
-		buf32 = next_entry(fp, sizeof(uint32_t) * items2);
-		if (!buf32) {
+		rc = next_entry(buf32, fp, sizeof(uint32_t) * items2);
+		if (rc < 0) {
 			ERR(fp->handle, "truncated entry");
 			return -1;
 		}
@@ -400,7 +400,7 @@
 			return -1;
 		}
 
-		for (i = 0; i < sizeof(spec_order) / sizeof(uint16_t); i++) {
+		for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
 			if (val & spec_order[i]) {
 				key.specified = spec_order[i] | enabled;
 				datum.data = le32_to_cpu(buf32[items++]);
@@ -418,8 +418,8 @@
 		return 0;
 	}
 
-	buf16 = next_entry(fp, sizeof(uint16_t) * 4);
-	if (!buf16) {
+	rc = next_entry(buf16, fp, sizeof(uint16_t) * 4);
+	if (rc < 0) {
 		ERR(fp->handle, "truncated entry");
 		return -1;
 	}
@@ -430,7 +430,7 @@
 	key.specified = le16_to_cpu(buf16[items++]);
 
 	set = 0;
-	for (i = 0; i < sizeof(spec_order) / sizeof(uint16_t); i++) {
+	for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
 		if (key.specified & spec_order[i])
 			set++;
 	}
@@ -439,8 +439,8 @@
 		return -1;
 	}
 
-	buf32 = next_entry(fp, sizeof(uint32_t));
-	if (!buf32) {
+	rc = next_entry(buf32, fp, sizeof(uint32_t));
+	if (rc < 0) {
 		ERR(fp->handle, "truncated entry");
 		return -1;
 	}
@@ -458,11 +458,11 @@
 {
 	unsigned int i;
 	int rc;
-	uint32_t *buf;
+	uint32_t buf[1];
 	uint32_t nel;
 
-	buf = next_entry(fp, sizeof(uint32_t));
-	if (!buf) {
+	rc = next_entry(buf, fp, sizeof(uint32_t));
+	if (rc < 0) {
 		ERR(fp->handle, "truncated table");
 		goto bad;
 	}
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.
#16 lore raw

Re: [patch] libsepol: eliminate unaligned accesses (Was: Re: wrong magic number (using old sources))

Not sure why I went over this patch, but looks pretty good 5 or so comments inline.

*****policydb.c

libsepol/src/policydb.c line 2700

Quoted 6 lines
libsepol/src/policydb.c +1 -1
@@ -2683,12 +2697,13 @@
 static int range_trans_rule_read(range_trans_rule_t ** r,
                                 struct policy_file *fp)
 {
-       uint32_t *buf, nel;
+       uint32_t buf[2], nel;

why buf[2] ? looks like buf[1] would suffice.

Quoted 15 lines
libsepol/src/policydb.c +3 -2
        unsigned int i;
        range_trans_rule_t *rt, *lrt = NULL;
+       int rc;
 
-       buf = next_entry(fp, sizeof(uint32_t));
-       if (!buf)
+       rc = next_entry(buf, fp, sizeof(uint32_t));
+       if (rc < 0)
                return -1;
        nel = le32_to_cpu(buf[0]);
        for (i = 0; i < nel; i++) {

libsepol/src/policydb.c line 2821

Quoted 5 lines
libsepol/src/policydb.c +1 -1
@@ -2799,11 +2818,12 @@
                             struct policy_file *fp)
 {
        avrule_block_t *last_block = NULL, *curblock;
-       uint32_t *buf, num_blocks, nel;
+       uint32_t buf[2], num_blocks, nel;

Another why buf[2]

libsepol/src/policydb.c line 2829

Quoted 10 lines
libsepol/src/policydb.c +3 -2
+       int rc;
 
-       if ((buf = next_entry(fp, sizeof(uint32_t))) == NULL) {
+       rc = next_entry(buf, fp, sizeof(uint32_t));
+       if (rc < 0)
                return -1;
-       }
        num_blocks = le32_to_cpu(buf[0]);
        nel = num_blocks;
        while (num_blocks > 0) {

******module.c

libsepol/src/module.c line 421

Quoted 6 lines
libsepol/src/module.c +1 -1
@@ -415,9 +418,9 @@
                              struct sepol_policy_file *spf, int verbose)
 {
        struct policy_file *file = &spf->pf;
-       uint32_t *buf, nsec;
+       uint32_t buf[5], nsec;

why buf[5] isn't 1 enough here?

Quoted 9 lines
libsepol/src/module.c +1 -1
        size_t *offsets, len;
-       int retval = -1;
+       int rc;
        unsigned i, seen = 0;
 
        if (module_package_read_offsets(mod, file, &offsets, &nsec))

libsepol/src/module.c line 599

Quoted 5 lines
libsepol/src/module.c +1 -1
@@ -593,7 +596,7 @@
 
       cleanup:
        free(offsets);
-       return retval;
+       return -1;

why throw away rc? just generally useless here?

Quoted 10 lines
libsepol/src/module.c +0 -0
 }
 int sepol_module_package_info(struct sepol_policy_file *spf, int *type,
 

Index: trunk/libsepol/src/avtab.c
===================================================================

libsepol/src/avtab.c (working copy) line 341

Quoted 7 lines
libsepol/src/avtab.c (working copy) renamed from libsepol/src/avtab.c (revision 2520) +2 -2
@@ -337,8 +337,8 @@
                    int (*insertf) (avtab_t * a, avtab_key_t * k,
                                    avtab_datum_t * d, void *p), void *p)
 {
-       uint16_t *buf16, enabled;
-       uint32_t *buf32, items, items2, val;
+       uint16_t buf16[4], enabled;
+       uint32_t buf32[7], items, items2, val;

looks like buf32 was allowed to be up to 8 but now only 7. Just know something about how this will be used that you know 7 is enough? I didn't dig into it.

Quoted 19 lines
libsepol/src/avtab.c (working copy) renamed from libsepol/src/avtab.c (revision 2520) +3 -3
        avtab_key_t key;
        avtab_datum_t datum;
        unsigned set;
@@ -349,20 +349,20 @@
        memset(&datum, 0, sizeof(avtab_datum_t));
 
        if (vers < POLICYDB_VERSION_AVTAB) {
-               buf32 = next_entry(fp, sizeof(uint32_t));
-               if (!buf32) {
+               rc = next_entry(buf32, fp, sizeof(uint32_t));
+               if (rc < 0) {
                        ERR(fp->handle, "truncated entry");
                        return -1;
                }
                items2 = le32_to_cpu(buf32[0]);
 
-               if (items2 < 5 || items2 > 8) {
+               if (items2 < 5 || items2 > ARRAY_SIZE(buf32)) {
                        ERR(fp->handle, "invalid item count");

libsepol/src/avtab.c (working copy) line 368

Quoted 10 lines
libsepol/src/avtab.c (working copy) renamed from libsepol/src/avtab.c (revision 2520) +2 -2
                        return -1;
                }
 
-               buf32 = next_entry(fp, sizeof(uint32_t) * items2);
-               if (!buf32) {
+               rc = next_entry(buf32, fp, sizeof(uint32_t) * items2);
+               if (rc < 0) {
                        ERR(fp->handle, "truncated entry");
                        return -1;
                }

--
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.

#17 lore raw

Re: [patch] libsepol: eliminate unaligned accesses (Was: Re: wrong magic number (using old sources))

Quoted 5 lines · Eric Paris

Not sure why I went over this patch, but looks pretty good 5 or so comments inline.

*****policydb.c

Quoted 8 lines

libsepol/src/policydb.c line 2700

Quoted 6 lines
libsepol/src/policydb.c +1 -1
@@ -2683,12 +2697,13 @@
 static int range_trans_rule_read(range_trans_rule_t ** r,
                                 struct policy_file *fp)
 {
-       uint32_t *buf, nel;
+       uint32_t buf[2], nel;

why buf[2] ? looks like buf[1] would suffice.

True, fixed. Cut-and-paste.

Quoted 14 lines
Quoted 11 lines
libsepol/src/policydb.c +3 -2
        unsigned int i;
        range_trans_rule_t *rt, *lrt = NULL;
+       int rc;
 
-       buf = next_entry(fp, sizeof(uint32_t));
-       if (!buf)
+       rc = next_entry(buf, fp, sizeof(uint32_t));
+       if (rc < 0)
                return -1;
        nel = le32_to_cpu(buf[0]);
        for (i = 0; i < nel; i++) {
Quoted 8 lines

libsepol/src/policydb.c line 2821

Quoted 6 lines
libsepol/src/policydb.c +1 -1
@@ -2799,11 +2818,12 @@
                             struct policy_file *fp)
 {
        avrule_block_t *last_block = NULL, *curblock;
-       uint32_t *buf, num_blocks, nel;
+       uint32_t buf[2], num_blocks, nel;

Another why buf[2]

Ditto.

Quoted 15 lines

libsepol/src/policydb.c line 2829

Quoted 11 lines
libsepol/src/policydb.c +3 -2
+       int rc;
 
-       if ((buf = next_entry(fp, sizeof(uint32_t))) == NULL) {
+       rc = next_entry(buf, fp, sizeof(uint32_t));
+       if (rc < 0)
                return -1;
-       }
        num_blocks = le32_to_cpu(buf[0]);
        nel = num_blocks;
        while (num_blocks > 0) {

******module.c

Quoted 8 lines

libsepol/src/module.c line 421

Quoted 6 lines
libsepol/src/module.c +1 -1
@@ -415,9 +418,9 @@
                              struct sepol_policy_file *spf, int verbose)
 {
        struct policy_file *file = &spf->pf;
-       uint32_t *buf, nsec;
+       uint32_t buf[5], nsec;

why buf[5] isn't 1 enough here?

Ditto.

Quoted 9 lines
Quoted 7 lines
libsepol/src/module.c +1 -1
        size_t *offsets, len;
-       int retval = -1;
+       int rc;
        unsigned i, seen = 0;
 
        if (module_package_read_offsets(mod, file, &offsets, &nsec))
Quoted 7 lines

libsepol/src/module.c line 599

Quoted 6 lines
libsepol/src/module.c +1 -1
@@ -593,7 +596,7 @@
 
       cleanup:
        free(offsets);
-       return retval;
+       return -1;

why throw away rc? just generally useless here?

rc gets overwritten as we read each section of the package, and we want to ensure that we don't ultimately return success when we failed. And it wasn't useful to preserve a distinct return value anyway.

Quoted 5 lines
Quoted 2 lines
libsepol/src/module.c +0 -0
 }
 int sepol_module_package_info(struct sepol_policy_file *spf, int *type,
Quoted 16 lines

libsepol/src/avtab.c (working copy) line 341

Quoted 12 lines

Index: trunk/libsepol/src/avtab.c
===================================================================

libsepol/src/avtab.c (working copy) renamed from libsepol/src/avtab.c (revision 2520) +2 -2
@@ -337,8 +337,8 @@
                    int (*insertf) (avtab_t * a, avtab_key_t * k,
                                    avtab_datum_t * d, void *p), void *p)
 {
-       uint16_t *buf16, enabled;
-       uint32_t *buf32, items, items2, val;
+       uint16_t buf16[4], enabled;
+       uint32_t buf32[7], items, items2, val;

looks like buf32 was allowed to be up to 8 but now only 7. Just know something about how this will be used that you know 7 is enough? I didn't dig into it.

The old code was wrong (and the new code is consistent with the kernel).

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.
#18 lore raw

Re: [patch] libsepol: eliminate unaligned accesses (Was: Re: wrong magic number (using old sources))

In reply to Stephen Smalley
Quoted 8 lines · Stephen Smalley

Full patch follows, replaces the prior short term one.

Rewrite libsepol next_entry function and all callers to copy entry data from the binary policy into properly aligned buffers, eliminating unaligned accesses, just as I did for the kernel back in 2004,
http://marc.info/?l=selinux&m=110252376515271&w=2

I merged the updated version of this patch to trunk and stable (the latter since it is a bug fix).

Note that this would ultimately have to get merged to policyrep as well, although it no doubt conflicts with your patches removing the module support from libsepol.

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.