Skip to contentSkip to conversation

x86: Preserve iopl on fork and execve

PATCH In review Alex Henrie 7 participants 11 messages May 11, 2015 – May 15, 2015 x86

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86: Preserve iopl on fork and execve

---
 arch/x86/kernel/process_32.c | 2 +-
 arch/x86/kernel/process_64.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/process_32.c +1 -1
index 8ed2106..0ef7078 100644
@@ -205,7 +205,7 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
 	regs->cs		= __USER_CS;
 	regs->ip		= new_ip;
 	regs->sp		= new_sp;
-	regs->flags		= X86_EFLAGS_IF;
+	regs->flags		= X86_EFLAGS_IF | (X86_EFLAGS_IOPL & regs->flags);
 	force_iret();
 }
 EXPORT_SYMBOL_GPL(start_thread);
arch/x86/kernel/process_64.c +1 -1
index ddfdbf7..e21eda2 100644
@@ -238,7 +238,7 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip,
 	regs->sp		= new_sp;
 	regs->cs		= _cs;
 	regs->ss		= _ss;
-	regs->flags		= X86_EFLAGS_IF;
+	regs->flags		= X86_EFLAGS_IF | (X86_EFLAGS_IOPL & regs->flags);
 	force_iret();
 }
 
Signature · 1 line
2.4.0
#1 lore raw
Quoted 25 lines · Alex Henrie
---
 arch/x86/kernel/process_32.c | 2 +-
 arch/x86/kernel/process_64.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/process_32.c +1 -1
index 8ed2106..0ef7078 100644
@@ -205,7 +205,7 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
 	regs->cs		= __USER_CS;
 	regs->ip		= new_ip;
 	regs->sp		= new_sp;
-	regs->flags		= X86_EFLAGS_IF;
+	regs->flags		= X86_EFLAGS_IF | (X86_EFLAGS_IOPL & regs->flags);
 	force_iret();
 }
 EXPORT_SYMBOL_GPL(start_thread);
arch/x86/kernel/process_64.c +0 -0
index ddfdbf7..e21eda2 100644

arch/x86/kernel/process_64.c start_thread_common(struct pt_regs *regs, unsigned long new_ip, · line 243

Quoted 7 lines
arch/x86/kernel/process_64.c +1 -1
@@ -238,7 +238,7 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip,
 	regs->sp		= new_sp;
 	regs->cs		= _cs;
 	regs->ss		= _ss;
-	regs->flags		= X86_EFLAGS_IF;
+	regs->flags		= X86_EFLAGS_IF | (X86_EFLAGS_IOPL & regs->flags);
 	force_iret();
 }

Yeah, NAK.

So this patch could be an instant roothole on some setups: assume old 64-bit apps relying on fork/clone/execve effectively flushing these capabilities and we'll now leak powerful hardware access permissions into child contexts that never had it before ...

I realize that this is a 2.5+ years old regression on 32-bit x86, and that the prior inheritance of iopl/ioperm was broken accidentally on 32-bit kernels by:

6783eaa2e125 ("x86, um/x86: switch to generic sys_execve and kernel_execve")

My arguments in favor of doing nothing are:

- Nothing actually broke that people cared about in the last 2.5 years, thus this might be one of the (very very rare) cases where preserving a breakage is the right thing to do.

- There's no reason to export this behavior to 64-bit x86 which apparently never had the iopl/ioperm capabilities propagation.

- Furthermore, even new 32-bit apps might have (accidentally) learned the new ABI, and we'd now break _them_, possibly in subtle ways.

- Plus iopl() and ioperm() are one of the most dangerous kernel APIs we have and the accidental limiting of them, which we got away with for 2.5+ years without being reportd, might just be what we want to stick with. An aspect of an API is only an ABI if it's actually used by applications.

- These syscalls are rarely used, and we could as well insist that every new context should have the permissions to (re-)acquire them and should actively seek them - instead of inheriting it to shells via system(), etc. The best strategy with dangerous APIs is to make it really, really explicit when they are used.

Permission propagation breakages like this are a rare situation and there's really no good way to fix them: damned if you do, damned if you don't.

So without far more analysis and far more care (a zero-length changelog won't cut it!) I doubt we can - or event want to - do anything like this...

Thanks,

Ingo

#2 lore raw
Quoted 4 lines · Ingo Molnar

- Nothing actually broke that people cared about in the last 2.5 years, thus this might be one of the (very very rare) cases where preserving a breakage is the right thing to do.

Indeed. The Linux "no regressions" rule is not about some theoretical "the ABI changed". It's about actual observed regressions.

So if we can improve the ABI without any user program or workflow breaking, that's fine.

How was this detected? Was it just from code inspection? Because if so, I think the "don't preserve iopl" is indeed the better ABI and we should keep it, accidental or not, since it restricts the impact.

But if it turns out somebody was actually depending on it, it's a regression. Of course, 2.5 years later, that is unlikely, but hey, some usages clearly end up updating kernels much too seldom.

Linus

#3 lore raw
Quoted 6 lines · Linus Torvalds
Quoted 4 lines · Ingo Molnar

- Nothing actually broke that people cared about in the last 2.5 years, thus this might be one of the (very very rare) cases where preserving a breakage is the right thing to do.

Quoted 6 lines

Indeed. The Linux "no regressions" rule is not about some theoretical "the ABI changed". It's about actual observed regressions.

So if we can improve the ABI without any user program or workflow breaking, that's fine.

But Linus, that would break dominix... ;)

-hpa

#4 lore raw
In reply to Ingo Molnar
Quoted 10 lines · Ingo Molnar

- Nothing actually broke that people cared about in the last 2.5 years, thus this might be one of the (very very rare) cases where preserving a breakage is the right thing to do.

- These syscalls are rarely used, and we could as well insist that every new context should have the permissions to (re-)acquire them and should actively seek them - instead of inheriting it to shells via system(), etc. The best strategy with dangerous APIs is to make it really, really explicit when they are used.

since nothing really broke and its a "nasty either way" regression wise, picking the more secure path looks the most sane.

the most likely impact path is in the X world, where X normally gets iopl type permissions (even thought it doesn't need
them anymore nowadays).. reverting this behavior would give all the processes X spawns off those perms as well...

also the interesting question is:
can a process give up these perms?
otherwise it becomes a "once given, never gotten rid of" hell hole.

#5 lore raw
Quoted 4 lines

also the interesting question is:
can a process give up these perms?
otherwise it becomes a "once given, never gotten rid of" hell hole.

If you look at a modern linux distro, nothing should need/use iopl and co anymore, so maybe an interesting
question is if we can stick these behind a CONFIG_ option (default on of course for compatibility)... just like
some of the /dev/mem like things are now hidable for folks who know they don't need them.

#6 lore raw
Quoted 11 lines · Arjan van de Ven
Quoted 4 lines

also the interesting question is:
can a process give up these perms?
otherwise it becomes a "once given, never gotten rid of" hell hole.

If you look at a modern linux distro, nothing should need/use iopl and co anymore, so maybe an interesting
question is if we can stick these behind a CONFIG_ option (default on of course for compatibility)... just like
some of the /dev/mem like things are now hidable for folks who know they don't need them.

Personally, I _really_ like this idea. The only thing I know of on any modern distro that even considers using ioperm is hwclock, and it only does so if it can't access the RTC through other means (and if you have an RTC, you really should have the /dev interface enabled).

#7 lore raw

2015-05-12 9:47 GMT-06:00 Austin S Hemmelgarn <[email protected]>:

Quoted 8 lines · Arjan van de Ven
Quoted 6 lines

If you look at a modern linux distro, nothing should need/use iopl and co anymore, so maybe an interesting
question is if we can stick these behind a CONFIG_ option (default on of course for compatibility)... just like
some of the /dev/mem like things are now hidable for folks who know they don't need them.

Quoted 4 lines

Personally, I _really_ like this idea. The only thing I know of on any modern distro that even considers using ioperm is hwclock, and it only does so if it can't access the RTC through other means (and if you have an RTC, you really should have the /dev interface enabled).

Removing iopl might be OK. Removing ioperm would break my use case of legacy code that needs direct access to the parallel port.

-Alex

#8 lore raw
Quoted 14 lines · Alex Henrie

2015-05-12 9:47 GMT-06:00 Austin S Hemmelgarn <[email protected]>:

Quoted 8 lines · Arjan van de Ven
Quoted 6 lines

If you look at a modern linux distro, nothing should need/use iopl and co anymore, so maybe an interesting
question is if we can stick these behind a CONFIG_ option (default on of course for compatibility)... just like
some of the /dev/mem like things are now hidable for folks who know they don't need them.

Quoted 4 lines

Personally, I _really_ like this idea. The only thing I know of on any modern distro that even considers using ioperm is hwclock, and it only does so if it can't access the RTC through other means (and if you have an RTC, you really should have the /dev interface enabled).

Quoted 5 lines

Removing iopl might be OK. Removing ioperm would break my use case of legacy code that needs direct access to the parallel port.

-Alex

The discussion isn't about outright removing them, just providing a config option to disable them. It might be a good idea though to provide separate config options for each of iopl() and ioperm(), as iopl() is more dangerous, and ioperm() is more widely used, and people may need one but not want to have the other.

#9 lore raw
In reply to Arjan van de Ven
Quoted 10 lines · Arjan van de Ven
Quoted 3 lines

also the interesting question is:
can a process give up these perms?
otherwise it becomes a "once given, never gotten rid of" hell hole.

If you look at a modern linux distro, nothing should need/use iopl and co anymore, so maybe an interesting
question is if we can stick these behind a CONFIG_ option (default on of course for compatibility)... just like
some of the /dev/mem like things are now hidable for folks who know they don't need them.

I have a patch series that does exactly that, compiling out the syscalls as well as the underlying architecture-specific infrastructure. (Saves quite a bit of space, too.)

It still needs some more detailed x86 architecture review. Peter, Ingo? Would you be interested in taking (an updated version of) that patch series for the next merge window?

- Josh Triplett

#10 lore raw
Quoted 9 lines · Josh Triplett

I have a patch series that does exactly that, compiling out the syscalls as well as the underlying architecture-specific infrastructure. (Saves quite a bit of space, too.)

It still needs some more detailed x86 architecture review. Peter, Ingo? Would you be interested in taking (an updated version of) that patch series for the next merge window?

I think that makes sense.

-hpa