2014-06-13 15:04:03

by Pawel Moll

[permalink] [raw]
Subject: [PATCH] perf: Handle compat ioctl

When running a 32-bit userspace on a 64-bit kernel (eg. i386
application on x86_64 kernel or 32-bit arm userspace on arm64
kernel) some of the perf ioctls must be treated with special
care, as they have a pointer size encoded in the command.

For example, PERF_EVENT_IOC_ID in 32-bit world will be encoded
as 0x80042407, but 64-bit kernel will expect 0x80082407. In
result the ioctl will fail returning -ENOTTY.

This patch solves the problem by adding code fixing up the
size as compat_ioctl file operation.

Reported-by: Drew Richardson <[email protected]>
Signed-off-by: Pawel Moll <[email protected]>
---
kernel/events/core.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 24d35cc..967af2c 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -3700,6 +3700,26 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
return 0;
}

+#ifdef CONFIG_COMPAT
+static long perf_compat_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ switch (_IOC_NR(cmd)) {
+ case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
+ case _IOC_NR(PERF_EVENT_IOC_ID):
+ /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
+ if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
+ cmd &= ~IOCSIZE_MASK;
+ cmd |= sizeof(void *) << IOCSIZE_SHIFT;
+ }
+ break;
+ }
+ return perf_ioctl(file, cmd, arg);
+}
+#else
+#define perf_compat_ioctl NULL
+#endif
+
int perf_event_task_enable(void)
{
struct perf_event *event;
@@ -4205,7 +4225,7 @@ static const struct file_operations perf_fops = {
.read = perf_read,
.poll = perf_poll,
.unlocked_ioctl = perf_ioctl,
- .compat_ioctl = perf_ioctl,
+ .compat_ioctl = perf_compat_ioctl,
.mmap = perf_mmap,
.fasync = perf_fasync,
};
--
1.9.1


2014-06-17 12:13:45

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH] perf: Handle compat ioctl

On Fri, Jun 13, 2014 at 04:03:32PM +0100, Pawel Moll wrote:
> When running a 32-bit userspace on a 64-bit kernel (eg. i386
> application on x86_64 kernel or 32-bit arm userspace on arm64
> kernel) some of the perf ioctls must be treated with special
> care, as they have a pointer size encoded in the command.
>
> For example, PERF_EVENT_IOC_ID in 32-bit world will be encoded
> as 0x80042407, but 64-bit kernel will expect 0x80082407. In
> result the ioctl will fail returning -ENOTTY.
>
> This patch solves the problem by adding code fixing up the
> size as compat_ioctl file operation.
>
> Reported-by: Drew Richardson <[email protected]>
> Signed-off-by: Pawel Moll <[email protected]>
> ---

This gets me (on my favourite x86_64 .config):

kernel/events/core.c: In function ‘perf_compat_ioctl’:
kernel/events/core.c:3726:32: error: ‘compat_uptr_t’ undeclared (first use in this function)
kernel/events/core.c:3726:32: note: each undeclared identifier is reported only once for each function it appears in


Attachments:
(No filename) (1.00 kB)
(No filename) (836.00 B)
Download all attachments

2014-06-17 12:44:05

by Pawel Moll

[permalink] [raw]
Subject: [PATCH v2] perf: Handle compat ioctl

When running a 32-bit userspace on a 64-bit kernel (eg. i386
application on x86_64 kernel or 32-bit arm userspace on arm64
kernel) some of the perf ioctls must be treated with special
care, as they have a pointer size encoded in the command.

For example, PERF_EVENT_IOC_ID in 32-bit world will be encoded
as 0x80042407, but 64-bit kernel will expect 0x80082407. In
result the ioctl will fail returning -ENOTTY.

This patch solves the problem by adding code fixing up the
size as compat_ioctl file operation.

Reported-by: Drew Richardson <[email protected]>
Signed-off-by: Pawel Moll <[email protected]>
---
kernel/events/core.c | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)

Changes from v1:
- added missing #include

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 5fa58e4..f081335 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -40,6 +40,7 @@
#include <linux/mm_types.h>
#include <linux/cgroup.h>
#include <linux/module.h>
+#include <linux/compat.h>

#include "internal.h"

@@ -3716,6 +3717,26 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
return 0;
}

+#ifdef CONFIG_COMPAT
+static long perf_compat_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ switch (_IOC_NR(cmd)) {
+ case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
+ case _IOC_NR(PERF_EVENT_IOC_ID):
+ /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
+ if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
+ cmd &= ~IOCSIZE_MASK;
+ cmd |= sizeof(void *) << IOCSIZE_SHIFT;
+ }
+ break;
+ }
+ return perf_ioctl(file, cmd, arg);
+}
+#else
+#define perf_compat_ioctl NULL
+#endif
+
int perf_event_task_enable(void)
{
struct perf_event *event;
@@ -4221,7 +4242,7 @@ static const struct file_operations perf_fops = {
.read = perf_read,
.poll = perf_poll,
.unlocked_ioctl = perf_ioctl,
- .compat_ioctl = perf_ioctl,
+ .compat_ioctl = perf_compat_ioctl,
.mmap = perf_mmap,
.fasync = perf_fasync,
};
--
1.9.1

2014-06-17 13:19:11

by Pawel Moll

[permalink] [raw]
Subject: Re: [PATCH] perf: Handle compat ioctl

On Tue, 2014-06-17 at 13:13 +0100, Peter Zijlstra wrote:
> On Fri, Jun 13, 2014 at 04:03:32PM +0100, Pawel Moll wrote:
> > When running a 32-bit userspace on a 64-bit kernel (eg. i386
> > application on x86_64 kernel or 32-bit arm userspace on arm64
> > kernel) some of the perf ioctls must be treated with special
> > care, as they have a pointer size encoded in the command.
> >
> > For example, PERF_EVENT_IOC_ID in 32-bit world will be encoded
> > as 0x80042407, but 64-bit kernel will expect 0x80082407. In
> > result the ioctl will fail returning -ENOTTY.
> >
> > This patch solves the problem by adding code fixing up the
> > size as compat_ioctl file operation.
> >
> > Reported-by: Drew Richardson <[email protected]>
> > Signed-off-by: Pawel Moll <[email protected]>
> > ---
>
> This gets me (on my favourite x86_64 .config):
>
> kernel/events/core.c: In function ‘perf_compat_ioctl’:
> kernel/events/core.c:3726:32: error: ‘compat_uptr_t’ undeclared (first use in this function)
> kernel/events/core.c:3726:32: note: each undeclared identifier is reported only once for each function it appears in

Right, sorry. I've added the size check last minute and haven't
re-tested it with x86_64. #include for compat.h was missing (wonder
where was it included for arm64 ;-)

Already posted v2.

Pawel

2014-07-01 16:06:02

by Pawel Moll

[permalink] [raw]
Subject: Re: [PATCH v2] perf: Handle compat ioctl

On Tue, 2014-06-17 at 13:43 +0100, Pawel Moll wrote:
> When running a 32-bit userspace on a 64-bit kernel (eg. i386
> application on x86_64 kernel or 32-bit arm userspace on arm64
> kernel) some of the perf ioctls must be treated with special
> care, as they have a pointer size encoded in the command.
>
> For example, PERF_EVENT_IOC_ID in 32-bit world will be encoded
> as 0x80042407, but 64-bit kernel will expect 0x80082407. In
> result the ioctl will fail returning -ENOTTY.
>
> This patch solves the problem by adding code fixing up the
> size as compat_ioctl file operation.
>
> Reported-by: Drew Richardson <[email protected]>
> Signed-off-by: Pawel Moll <[email protected]>

This just is a polite and friendly nag...

Any, strong or not, opinions on the matter?

Cheers!

Pawel

2014-07-07 12:43:29

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH v2] perf: Handle compat ioctl

On Tue, Jul 01, 2014 at 05:06:00PM +0100, Pawel Moll wrote:
> On Tue, 2014-06-17 at 13:43 +0100, Pawel Moll wrote:
> > When running a 32-bit userspace on a 64-bit kernel (eg. i386
> > application on x86_64 kernel or 32-bit arm userspace on arm64
> > kernel) some of the perf ioctls must be treated with special
> > care, as they have a pointer size encoded in the command.
> >
> > For example, PERF_EVENT_IOC_ID in 32-bit world will be encoded
> > as 0x80042407, but 64-bit kernel will expect 0x80082407. In
> > result the ioctl will fail returning -ENOTTY.
> >
> > This patch solves the problem by adding code fixing up the
> > size as compat_ioctl file operation.
> >
> > Reported-by: Drew Richardson <[email protected]>
> > Signed-off-by: Pawel Moll <[email protected]>
>
> This just is a polite and friendly nag...
>
> Any, strong or not, opinions on the matter?

Sorry, travel, cracks, falling, etc..

Queued it, we'll see what happens with this one ;-)


Attachments:
(No filename) (971.00 B)
(No filename) (836.00 B)
Download all attachments