Skip to contentSkip to conversation

ioctl help

DISCUSSION Dominick Grift 3 participants 6 messages May 24, 2017 – May 25, 2017 SELinux security module

lore
Share & tools
lore
#0 lore raw

I was looking again at ioctl whitelisting, and excuse me if I overlooked some documentation, but I am having a hard time implementing this. what I did was I just wanted to basically test blacklisting a single ioctl (no particular one)

So i looked into androids sepolicy and just picked a semi-random ioctl from their "https://android.googlesource.com/platform/system/sepolicy/+/master/public/ioctl_defines"

for example: PHONE_CAPABILITIES_CHECK 0x40087182

However the xpermissions statement only allows 0x0000 to 0xFFFF when i tried: (xpermission alg_socket_ioctl (ioctl alg_socket (not (0x40087182))))

My question is how do i convert these to something i can use with the xpermission statement in CIL, and why can seandroid sepolicy get away with using 0x12345678 where i have to use 0x1234? I could not find any scripts that converts these in the android tree.

Signature · 3 lines
Key fingerprint = 5F4D 3CDB D3F8 3652 FBD8  02D5 3B6C 5F1D 2C7B 6B02
https://sks-keyservers.net/pks/lookup?op=get&search=0x3B6C5F1D2C7B6B02
Dominick Grift
#1 lore raw
Quoted 12 lines · Dominick Grift

I was looking again at ioctl whitelisting, and excuse me if I overlooked some documentation, but I am having a hard time implementing this.
what I did was I just wanted to basically test blacklisting a single ioctl (no particular one)

So i looked into androids sepolicy and just picked a semi-random ioctl from their "https://android.googlesource.com/platform/system/se policy/+/master/public/ioctl_defines"

for example: PHONE_CAPABILITIES_CHECK 0x40087182

Quoted 8 lines

However the xpermissions statement only allows 0x0000 to 0xFFFF when i tried: (xpermission alg_socket_ioctl (ioctl alg_socket (not (0x40087182))))

My question is how do i convert these to something i can use with the xpermission statement in CIL, and why can seandroid sepolicy get away with using 0x12345678 where i have to use 0x1234? I could not find any scripts that converts these in the android tree.

FWIW, I added a simple test of ioctl whitelisting to the selinux- testsuite, although that was done in source policy and depends on the binary module format support for xperms.

With regard to your question though, only the low 16 bits of the ioctl value (the type/driver and number/function fields) are actually used; the upper 16 bits encode the direction (read/write) and size of any argument to the ioctl and are therefore not relevant for whitelisting. So you can just use 0x7182. checkpolicy just ignores the upper bits, which I guess is convenient so that they can use ioctl macro lists generated from kernel header definitions, and Android builds by using checkpolicy -C to convert policy.conf to CIL.

#2 lore raw
Quoted 26 lines · Stephen Smalley
Quoted 12 lines · Dominick Grift

I was looking again at ioctl whitelisting, and excuse me if I overlooked some documentation, but I am having a hard time implementing this.
what I did was I just wanted to basically test blacklisting a single ioctl (no particular one)

So i looked into androids sepolicy and just picked a semi-random ioctl from their "https://android.googlesource.com/platform/system/se policy/+/master/public/ioctl_defines"

for example: PHONE_CAPABILITIES_CHECK 0x40087182

Quoted 8 lines

However the xpermissions statement only allows 0x0000 to 0xFFFF when i tried: (xpermission alg_socket_ioctl (ioctl alg_socket (not (0x40087182))))

My question is how do i convert these to something i can use with the xpermission statement in CIL, and why can seandroid sepolicy get away with using 0x12345678 where i have to use 0x1234? I could not find any scripts that converts these in the android tree.

FWIW, I added a simple test of ioctl whitelisting to the selinux- testsuite, although that was done in source policy and depends on the binary module format support for xperms.

Quoted 8 lines

With regard to your question though, only the low 16 bits of the ioctl value (the type/driver and number/function fields) are actually used; the upper 16 bits encode the direction (read/write) and size of any argument to the ioctl and are therefore not relevant for whitelisting. So you can just use 0x7182. checkpolicy just ignores the upper bits, which I guess is convenient so that they can use ioctl macro lists generated from kernel header definitions, and Android builds by using checkpolicy -C to convert policy.conf to CIL.

Thanks. I considered that but then I thought I saw various different ioctls with the same last 16 bits so that got me confused

Signature · 3 lines
Key fingerprint = 5F4D 3CDB D3F8 3652 FBD8  02D5 3B6C 5F1D 2C7B 6B02
https://sks-keyservers.net/pks/lookup?op=get&search=0x3B6C5F1D2C7B6B02
Dominick Grift
#3 lore raw
Quoted 36 lines · Dominick Grift
Quoted 26 lines · Stephen Smalley
Quoted 12 lines · Dominick Grift

I was looking again at ioctl whitelisting, and excuse me if I overlooked some documentation, but I am having a hard time implementing this.
what I did was I just wanted to basically test blacklisting a single ioctl (no particular one)

So i looked into androids sepolicy and just picked a semi-random ioctl from their "https://android.googlesource.com/platform/system/se policy/+/master/public/ioctl_defines"

for example: PHONE_CAPABILITIES_CHECK 0x40087182

Quoted 8 lines

However the xpermissions statement only allows 0x0000 to 0xFFFF when i tried: (xpermission alg_socket_ioctl (ioctl alg_socket (not (0x40087182))))

My question is how do i convert these to something i can use with the xpermission statement in CIL, and why can seandroid sepolicy get away with using 0x12345678 where i have to use 0x1234? I could not find any scripts that converts these in the android tree.

FWIW, I added a simple test of ioctl whitelisting to the selinux- testsuite, although that was done in source policy and depends on the binary module format support for xperms.

Quoted 8 lines

With regard to your question though, only the low 16 bits of the ioctl value (the type/driver and number/function fields) are actually used; the upper 16 bits encode the direction (read/write) and size of any argument to the ioctl and are therefore not relevant for whitelisting. So you can just use 0x7182. checkpolicy just ignores the upper bits, which I guess is convenient so that they can use ioctl macro lists generated from kernel header definitions, and Android builds by using checkpolicy -C to convert policy.conf to CIL.

Quoted 1 line

Thanks. I considered that but then I thought I saw various different ioctls with the same last 16 bits so that got me confused

With the above in mind, how should i interpret the following

define(CHIOEXCHANGE', 0x401c6302')
define(CM_IOCSPTS', 0x40086302')

is "0x6203" CHIOEXCHANGE, is it CM_IOCSPTS, or are the two the same with exception of direction and/or size

Quoted 5 lines
Signature · 4 lines
-- 
Key fingerprint = 5F4D 3CDB D3F8 3652 FBD8  02D5 3B6C 5F1D 2C7B 6B02
https://sks-keyservers.net/pks/lookup?op=get&search=0x3B6C5F1D2C7B6B02
Dominick Grift
Signature · 3 lines
Key fingerprint = 5F4D 3CDB D3F8 3652 FBD8  02D5 3B6C 5F1D 2C7B 6B02
https://sks-keyservers.net/pks/lookup?op=get&search=0x3B6C5F1D2C7B6B02
Dominick Grift
#4 lore raw

The command number is just a uint16. It's up to each driver to interpret it. So it's CHIOEXCHANGE for a particular driver and CM_IOCSPTS for a different driver.

Ideally, you're whitelisting ioctls on a per-driver basis, so this model works.

Quoted 62 lines · Dominick Grift
Quoted 10 lines · Dominick Grift
Quoted 9 lines · Stephen Smalley
Quoted 8 lines · Dominick Grift

I was looking again at ioctl whitelisting, and excuse me if I overlooked some documentation, but I am having a hard time implementing this.
what I did was I just wanted to basically test blacklisting a single ioctl (no particular one)

So i looked into androids sepolicy and just picked a semi-random ioctl from their "

https://android.googlesource.com/platform/system/se

Quoted 26 lines
Quoted 17 lines
Quoted 4 lines

policy/+/master/public/ioctl_defines"

for example: PHONE_CAPABILITIES_CHECK 0x40087182

Quoted 8 lines

However the xpermissions statement only allows 0x0000 to 0xFFFF when i tried: (xpermission alg_socket_ioctl (ioctl alg_socket (not (0x40087182))))

My question is how do i convert these to something i can use with the xpermission statement in CIL, and why can seandroid sepolicy get away with using 0x12345678 where i have to use 0x1234? I could not find any scripts that converts these in the android tree.

FWIW, I added a simple test of ioctl whitelisting to the selinux- testsuite, although that was done in source policy and depends on the binary module format support for xperms.

Quoted 8 lines

With regard to your question though, only the low 16 bits of the ioctl value (the type/driver and number/function fields) are actually used; the upper 16 bits encode the direction (read/write) and size of any argument to the ioctl and are therefore not relevant for whitelisting. So you can just use 0x7182. checkpolicy just ignores the upper bits, which I guess is convenient so that they can use ioctl macro lists generated from kernel header definitions, and Android builds by using checkpolicy -C to convert policy.conf to CIL.

Quoted 1 line

Thanks. I considered that but then I thought I saw various different

ioctls with the same last 16 bits so that got me confused

With the above in mind, how should i interpret the following

define(CHIOEXCHANGE', 0x401c6302')
define(CM_IOCSPTS', 0x40086302')

is "0x6203" CHIOEXCHANGE, is it CM_IOCSPTS, or are the two the same with exception of direction and/or size

Quoted 5 lines

--
Key fingerprint = 5F4D 3CDB D3F8 3652 FBD8 02D5 3B6C 5F1D 2C7B 6B02
https://sks-keyservers.net/pks/lookup?op=get&search=0x3B6C5F1D2C7B6B02
Dominick Grift

--
Key fingerprint = 5F4D 3CDB D3F8 3652 FBD8 02D5 3B6C 5F1D 2C7B 6B02
https://sks-keyservers.net/pks/lookup?op=get&search=0x3B6C5F1D2C7B6B02
Dominick Grift

#5 lore raw
Quoted 6 lines · Jeffrey Vander Stoep via Selinux

The command number is just a uint16. It's up to each driver to interpret it. So it's CHIOEXCHANGE for a particular driver and CM_IOCSPTS for a different driver.

Ideally, you're whitelisting ioctls on a per-driver basis, so this model works.

Possibly to avoid user confusion, we should have checkpolicy only accept values 0-0xffff (as with CIL), and the macros should be truncated when they are generated. Otherwise, reading the policy could be misleading.

Quoted 84 lines
Quoted 81 lines · Dominick Grift
Quoted 6 lines · Dominick Grift
Quoted 5 lines · Stephen Smalley
Quoted 4 lines · Dominick Grift

I was looking again at ioctl whitelisting, and excuse me if I overlooked some documentation, but I am having a hard time implementing this.
what I did was I just wanted to basically test blacklisting a

single

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

ioctl (no particular one)

So i looked into androids sepolicy and just picked a semi-

random

Quoted 1 line
Quoted 1 line
Quoted 1 line

ioctl from their "https://android.googlesource.com/platform/s

ystem/se

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

policy/+/master/public/ioctl_defines"

for example: PHONE_CAPABILITIES_CHECK 0x40087182

However the xpermissions statement only allows 0x0000 to

0xFFFF when

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

i tried: (xpermission alg_socket_ioctl (ioctl alg_socket (not (0x40087182))))

My question is how do i convert these to something i can use

with the

Quoted 1 line
Quoted 1 line
Quoted 1 line

xpermission statement in CIL, and why can seandroid sepolicy

get away

Quoted 1 line
Quoted 1 line
Quoted 1 line

with using 0x12345678 where i have to use 0x1234? I could not

find

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

any scripts that converts these in the android tree.

FWIW, I added a simple test of ioctl whitelisting to the

selinux-

Quoted 1 line
Quoted 1 line

testsuite, although that was done in source policy and depends

on the

Quoted 3 lines
Quoted 3 lines

binary module format support for xperms.

With regard to your question though, only the low 16 bits of

the ioctl

Quoted 1 line
Quoted 1 line

value (the type/driver and number/function fields) are actually

used;

Quoted 1 line
Quoted 1 line

the upper 16 bits encode the direction (read/write) and size of

any

Quoted 1 line
Quoted 1 line

argument to the ioctl and are therefore not relevant for

whitelisting.

Quoted 1 line
Quoted 1 line

So you can just use 0x7182.  checkpolicy just ignores the upper

bits,

Quoted 1 line
Quoted 1 line

which I guess is convenient so that they can use ioctl macro

lists

Quoted 1 line
Quoted 1 line

generated from kernel header definitions, and Android builds by

using

Quoted 3 lines
Quoted 1 line

checkpolicy -C to convert policy.conf to CIL.

Thanks. I considered that but then I thought I saw various

different ioctls with the same last 16 bits so that got me confused

With the above in mind, how should i interpret the following

define(CHIOEXCHANGE', 0x401c6302')
define(CM_IOCSPTS', 0x40086302')

is "0x6203" CHIOEXCHANGE, is it CM_IOCSPTS, or are the two the same with exception of direction and/or size

Quoted 3 lines

--
Key fingerprint = 5F4D 3CDB D3F8 3652 FBD8  02D5 3B6C 5F1D 2C7B

6B02

Quoted 1 line

https://sks-keyservers.net/pks/lookup?op=get&search=0x3B6C5F1D2C7

B6B02

Quoted 1 line

Dominick Grift

--
Key fingerprint = 5F4D 3CDB D3F8 3652 FBD8  02D5 3B6C 5F1D 2C7B 6B02
https://sks-keyservers.net/pks/lookup?op=get&search=0x3B6C5F1D2C7B6
B02
Dominick Grift