2015-05-11 23:38:36

by Alex Henrie

[permalink] [raw]
Subject: [PATCH v2] x86: Preserve iopl on fork and execve

Signed-off-by: Alex Henrie <[email protected]>
Suggested-by: Doug Johnson <[email protected]>
---
arch/x86/kernel/process_32.c | 2 +-
arch/x86/kernel/process_64.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/process_32.c b/arch/x86/kernel/process_32.c
index 8ed2106..0ef7078 100644
--- a/arch/x86/kernel/process_32.c
+++ b/arch/x86/kernel/process_32.c
@@ -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);
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index ddfdbf7..e21eda2 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -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();
}

--
2.4.0


2015-05-12 06:40:39

by Ingo Molnar

[permalink] [raw]
Subject: Re: [PATCH v2] x86: Preserve iopl on fork and execve


* Alex Henrie <[email protected]> wrote:

> Signed-off-by: Alex Henrie <[email protected]>
> Suggested-by: Doug Johnson <[email protected]>
> ---
> arch/x86/kernel/process_32.c | 2 +-
> arch/x86/kernel/process_64.c | 2 +-
> 2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/kernel/process_32.c b/arch/x86/kernel/process_32.c
> index 8ed2106..0ef7078 100644
> --- a/arch/x86/kernel/process_32.c
> +++ b/arch/x86/kernel/process_32.c
> @@ -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);
> diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
> index ddfdbf7..e21eda2 100644
> --- a/arch/x86/kernel/process_64.c
> +++ b/arch/x86/kernel/process_64.c
> @@ -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

2015-05-12 15:13:39

by Linus Torvalds

[permalink] [raw]
Subject: Re: [PATCH v2] x86: Preserve iopl on fork and execve

On Mon, May 11, 2015 at 11:40 PM, Ingo Molnar <[email protected]> wrote:
>
> - 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

2015-05-12 15:24:41

by Arjan van de Ven

[permalink] [raw]
Subject: Re: [PATCH v2] x86: Preserve iopl on fork and execve

On Mon, May 11, 2015 at 11:40 PM, Ingo Molnar <[email protected]> wrote:
> - 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.

2015-05-12 15:26:20

by Arjan van de Ven

[permalink] [raw]
Subject: Re: [PATCH v2] x86: Preserve iopl on fork and execve

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

2015-05-12 15:47:52

by Austin S Hemmelgarn

[permalink] [raw]
Subject: Re: [PATCH v2] x86: Preserve iopl on fork and execve

On 2015-05-12 11:25, Arjan van de Ven wrote:
>>
>> 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).



Attachments:
smime.p7s (2.90 kB)
S/MIME Cryptographic Signature

2015-05-12 18:06:09

by Alex Henrie

[permalink] [raw]
Subject: Re: [PATCH v2] x86: Preserve iopl on fork and execve

2015-05-12 9:47 GMT-06:00 Austin S Hemmelgarn <[email protected]>:
> On 2015-05-12 11:25, Arjan van de Ven wrote:
>> 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).

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

-Alex

2015-05-12 18:12:25

by Austin S Hemmelgarn

[permalink] [raw]
Subject: Re: [PATCH v2] x86: Preserve iopl on fork and execve

On 2015-05-12 14:05, Alex Henrie wrote:
> 2015-05-12 9:47 GMT-06:00 Austin S Hemmelgarn <[email protected]>:
>> On 2015-05-12 11:25, Arjan van de Ven wrote:
>>> 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).
>
> 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.


Attachments:
smime.p7s (2.90 kB)
S/MIME Cryptographic Signature

2015-05-12 18:25:34

by H. Peter Anvin

[permalink] [raw]
Subject: Re: [PATCH v2] x86: Preserve iopl on fork and execve

On 05/12/2015 08:13 AM, Linus Torvalds wrote:
> On Mon, May 11, 2015 at 11:40 PM, Ingo Molnar <[email protected]> wrote:
>>
>> - 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.
>

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

-hpa

2015-05-14 10:42:08

by Josh Triplett

[permalink] [raw]
Subject: Re: [PATCH v2] x86: Preserve iopl on fork and execve

On Tue, May 12, 2015 at 08:25:59AM -0700, Arjan van de Ven wrote:
> > 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

2015-05-15 00:53:06

by H. Peter Anvin

[permalink] [raw]
Subject: Re: [PATCH v2] x86: Preserve iopl on fork and execve

On 05/14/2015 03:41 AM, Josh Triplett wrote:
>
> 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