signal/usb: Replace kill_pid_info_as_cred with kill_pid_usb_asyncio
PATCH 1/1 signal/usb: Replace kill_pid_info_as_cred with kill_pid_usb_asyncio
The usb support for asyncio encoded one of it's values in the wrong field. It should have used si_value but instead used si_addr which is not present in the _rt union member of struct siginfo.
The result is a POSIX and glibc incompatible encoding of fields in struct siginfo with si_code of SI_ASYNCIO. This makes it impossible to look at a struct siginfo with si_code set to SI_ASYNCIO and without context properly decode it. Which unfortunately means that copy_siginfo_to_user32 can't handle the compat issues this unfortunate choice in encodings brings up.
Therefore replace kill_pid_info_as_cred with kill_pid_usb_asyncio a dedicated function for this one specific case. There are no other users of kill_pid_info_as_cred so this specialization should have no impact on the amont of code in the kernel. Have kill_pid_usb_asyncio take instead of a siginfo_t which is difficult error prone 3 arguments, a signal number, an errno value, and an address enconded as a sigval_t. The encoding as a sigval_t allows the caller to deal with the compat issues before calling kill_pid_info_as_cred.
Add BUILD_BUG_ONs in kernel/signal.c to ensure that we can now place the pointer value at the in si_pid (instead of si_addr) and get the same binary result when the structure is copied to user space and when the structure is copied field by field.
The usb code is updated to track if the values it passes into kill_pid_usb_asyncio were passed to it from a native userspace or from at compat user space. To allow a proper conversion of the addresses.
---
Can I get someone to test this code? I just discovered that the usb code is filling in struct siginfo wrong and copy_siginfo_to_user32 can't possibly get this correct without some help.
I think I have coded up a working fix. But I don't have a setup where I can test this.
drivers/usb/core/devio.c | 45 +++++++++++------------ include/linux/sched/signal.h | 2 +- kernel/signal.c | 69 +++++++++++++++++++++++++++++++----- 3 files changed, 83 insertions(+), 33 deletions(-)
drivers/usb/core/devio.c +21 -24
@@ -63,7 +63,7 @@ struct usb_dev_state {unsigned int discsignr;struct pid *disc_pid;const struct cred *cred; -void __user *disccontext; +sigval_t disccontext;unsigned long ifclaimed;u32 disabled_bulk_eps;bool privileges_dropped;
@@ -94,6 +94,7 @@ struct async {struct usb_memory *usbm;unsigned int mem_usage;int status; +u8 compat_userurb;u8 bulk_addr;u8 bulk_status; };
@@ -582,22 +583,24 @@ static void async_completed(struct urb *urb) {struct async *as = urb->context;struct usb_dev_state *ps = as->ps; -struct kernel_siginfo sinfo;struct pid *pid =NULL ;const struct cred *cred =NULL ;unsigned long flags; -int signr; +sigval_t addr; +int signr, errno;spin_lock_irqsave (&ps->lock, flags);list_move_tail (&as->asynclist, &ps->async_completed); as->status = urb->status; signr = as->signr;if (signr) { -clear_siginfo (&sinfo); - sinfo.si_signo = as->signr; - sinfo.si_errno = as->status; - sinfo.si_code =SI_ASYNCIO ; - sinfo.si_addr = as->userurb; + errno = as->status; +#ifdef CONFIG_COMPAT +if (as->compat_userurb) + addr.sival_int =ptr_to_compat (as->userurb); + else +#endif + addr.sival_ptr = as->userurb; pid =get_pid (as->pid); cred =get_cred (as->cred); }
@@ -615,7 +618,7 @@ static void async_completed(struct urb *urb)spin_unlock_irqrestore (&ps->lock, flags);if (signr) { -kill_pid_info_as_cred (sinfo.si_signo, &sinfo, pid, cred); +kill_pid_usb_asyncio (signr, errno, addr, pid, cred);put_pid (pid);put_cred (cred); }
@@ -1427,7 +1430,7 @@ find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)static int proc_do_submiturb (struct usb_dev_state *ps,struct usbdevfs_urb *uurb,struct usbdevfs_iso_packet_desc __user *iso_frame_desc, -void __user *arg) +void __user *arg,bool compat) {struct usbdevfs_iso_packet_desc *isopkt =NULL ;struct usb_host_endpoint *ep;
@@ -1729,6 +1732,7 @@ static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb isopkt =NULL ; as->ps = ps; as->userurb = arg; + as->compat_userurb = compat;if (as->usbm) {unsigned long uurb_start = (unsigned long )uurb->buffer;
@@ -1809,7 +1813,7 @@ static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)return proc_do_submiturb (ps, &uurb, (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), - arg); + arg, false); }static int proc_unlinkurb (struct usb_dev_state *ps,void __user *arg)
@@ -1979,7 +1983,7 @@ static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *aif (copy_from_user (&ds, arg,sizeof (ds)))return -EFAULT ; ps->discsignr = ds.signr; - ps->disccontext =compat_ptr (ds.context); + ps->disccontext.sival_int = ds.context;return 0 ; }
@@ -2013,7 +2017,7 @@ static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)return proc_do_submiturb (ps, &uurb, ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, - arg); + arg, true); }static int processcompl_compat (struct async *as,void __user *__user *arg)
@@ -2094,7 +2098,7 @@ static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)if (copy_from_user (&ds, arg,sizeof (ds)))return -EFAULT ; ps->discsignr = ds.signr; - ps->disccontext = ds.context; + ps->disccontext.sival_ptr = ds.context;return 0 ; }
@@ -2616,22 +2620,15 @@ const struct file_operations usbdev_file_operations = {static void usbdev_remove (struct usb_device *udev) {struct usb_dev_state *ps; -struct kernel_siginfo sinfo;while (!list_empty (&udev->filelist)) { ps = list_entry(udev->filelist.next, struct usb_dev_state, list);destroy_all_async (ps);wake_up_all (&ps->wait);list_del_init (&ps->list); -if (ps->discsignr) { -clear_siginfo (&sinfo); - sinfo.si_signo = ps->discsignr; - sinfo.si_errno =EPIPE ; - sinfo.si_code =SI_ASYNCIO ; - sinfo.si_addr = ps->disccontext; -kill_pid_info_as_cred (ps->discsignr, &sinfo, - ps->disc_pid, ps->cred); - } +if (ps->discsignr) +kill_pid_usb_asyncio (ps->discsignr,EPIPE , ps->disccontext, + ps->disc_pid, ps->cred); } }
include/linux/sched/signal.h +1 -1
@@ -330,7 +330,7 @@ extern void force_sigsegv(int sig);extern int force_sig_info (struct kernel_siginfo *);extern int __kill_pgrp_info (int sig,struct kernel_siginfo *info,struct pid *pgrp);extern int kill_pid_info (int sig,struct kernel_siginfo *info,struct pid *pid); -extern int kill_pid_info_as_cred (int ,struct kernel_siginfo *,struct pid *, +extern int kill_pid_usb_asyncio (int sig,int errno,sigval_t addr,struct pid *,const struct cred *);extern int kill_pgrp (struct pid *pid,int sig,int priv);extern int kill_pid (struct pid *pid,int sig,int priv);
kernel/signal.c +61 -8
104 lines not on this page · raw message
Quoted 37 lines · Eric W. Biederman
The usb support for asyncio encoded one of it's values in the wrong field. It should have used
si_valuebut instead usedsi_addrwhich is not present in the_rtunion member of struct siginfo.The result is a POSIX and glibc incompatible encoding of fields in struct siginfo with
si_codeofSI_ASYNCIO. This makes it impossible to look at a struct siginfo withsi_codeset toSI_ASYNCIOand without context properly decode it. Which unfortunately means thatcopy_siginfo_to_user32can't handle the compat issues this unfortunate choice in encodings brings up.Therefore replace
kill_pid_info_as_credwithkill_pid_usb_asyncioa dedicated function for this one specific case. There are no other users ofkill_pid_info_as_credso this specialization should have no impact on the amont of code in the kernel. Havekill_pid_usb_asynciotake instead of asiginfo_twhich is difficult error prone 3 arguments, a signal number, an errno value, and an address enconded as asigval_t. The encoding as asigval_tallows the caller to deal with the compat issues before callingkill_pid_info_as_cred.Add
BUILD_BUG_ONsinkernel/signal.cto ensure that we can now place the pointer value at the insi_pid(instead ofsi_addr) and get the same binary result when the structure is copied to user space and when the structure is copied field by field.The usb code is updated to track if the values it passes into
kill_pid_usb_asynciowere passed to it from a native userspace or from at compat user space. To allow a proper conversion of the addresses.Cc Greg Kroah-Hartman <[email protected]>Cc Alan Stern <[email protected]>Cc Oliver Neukum <[email protected]>Fixes v2.3.39Signed-off-by "Eric W. Biederman" <[email protected]>
Quoted 8 lines
---
Can I get someone to test this code? I just discovered that the usb code is filling in struct siginfo wrong and
copy_siginfo_to_user32can't possibly get this correct without some help.I think I have coded up a working fix. But I don't have a setup where I can test this.
Eric:
You should be able to test this patch by running the attached program. It takes one argument, the pathname to a USB device file. For example, on my system:
# ./usbsig /dev/bus/usb/001/001 Got signal 10, signo 10 errno 0 code -4
I don't know exactly what you're looking for, but it should be pretty easy to modify the test program however you want.
If you need to test the compatibility mode specifically, I can do that here -- I'm running a 32-bit userspace on a 64-bit kernel. But you'll have to tell me exactly what test code to run.
Alan Stern
/* usbsig.c -- test USB async signal delivery */
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/usb/ch9.h>
#include <linux/usbdevice_fs.h>
void handler(int sig, siginfo_t *info , void *ucontext)
{
ucontext_t *con = (ucontext_t *) ucontext;
printf("Got signal %d, signo %d errno %d code %d\n",
sig, info->si_signo, info->si_errno, info->si_code);
}int main(int argc, char **argv)
{
char *devfilename;
int fd;
int rc;
struct sigaction act;
struct usbdevfs_urb urb;
struct usb_ctrlrequest *req;
void *ptr;
char buf[80];
if (argc != 2) {
fprintf(stderr, "Usage: usbsig device-file-name\n");
return 1;
}
devfilename = argv[1];
fd = open(devfilename, O_RDWR);
if (fd == -1) {
perror("Error opening device file");
return 1;
}
act.sa_sigaction = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
rc = sigaction(SIGUSR1, &act, NULL);
if (rc == -1) {
perror("Error in sigaction");
return 1;
}
memset(&urb, 0, sizeof(urb));
urb.type = USBDEVFS_URB_TYPE_CONTROL;
urb.endpoint = USB_DIR_IN | 0;
urb.buffer = buf;
urb.buffer_length = sizeof(buf);
urb.signr = SIGUSR1;
req = (struct usb_ctrlrequest *) buf;
req->bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
req->bRequest = USB_REQ_GET_DESCRIPTOR;
req->wValue = USB_DT_DEVICE << 8;
req->wIndex = 0;
req->wLength = sizeof(buf) - sizeof(*req);
rc = ioctl(fd, USBDEVFS_SUBMITURB, &urb);
if (rc == -1) {
perror("Error in SUBMITURB ioctl");
return 1;
}
rc = ioctl(fd, USBDEVFS_REAPURB, &ptr);
if (rc == -1) {
perror("Error in REAPURB ioctl");
return 1;
}
close(fd);
return 0;
}Quoted 57 lines · Alan Stern
Quoted 37 lines · Eric W. Biederman
The usb support for asyncio encoded one of it's values in the wrong field. It should have used
si_valuebut instead usedsi_addrwhich is not present in the_rtunion member of struct siginfo.The result is a POSIX and glibc incompatible encoding of fields in struct siginfo with
si_codeofSI_ASYNCIO. This makes it impossible to look at a struct siginfo withsi_codeset toSI_ASYNCIOand without context properly decode it. Which unfortunately means thatcopy_siginfo_to_user32can't handle the compat issues this unfortunate choice in encodings brings up.Therefore replace
kill_pid_info_as_credwithkill_pid_usb_asyncioa dedicated function for this one specific case. There are no other users ofkill_pid_info_as_credso this specialization should have no impact on the amont of code in the kernel. Havekill_pid_usb_asynciotake instead of asiginfo_twhich is difficult error prone 3 arguments, a signal number, an errno value, and an address enconded as asigval_t. The encoding as asigval_tallows the caller to deal with the compat issues before callingkill_pid_info_as_cred.Add
BUILD_BUG_ONsinkernel/signal.cto ensure that we can now place the pointer value at the insi_pid(instead ofsi_addr) and get the same binary result when the structure is copied to user space and when the structure is copied field by field.The usb code is updated to track if the values it passes into
kill_pid_usb_asynciowere passed to it from a native userspace or from at compat user space. To allow a proper conversion of the addresses.Cc Greg Kroah-Hartman <[email protected]>Cc Alan Stern <[email protected]>Cc Oliver Neukum <[email protected]>Fixes v2.3.39Signed-off-by "Eric W. Biederman" <[email protected]>Quoted 8 lines
---
Can I get someone to test this code? I just discovered that the usb code is filling in struct siginfo wrong and
copy_siginfo_to_user32can't possibly get this correct without some help.I think I have coded up a working fix. But I don't have a setup where I can test this.
Eric:
You should be able to test this patch by running the attached program. It takes one argument, the pathname to a USB device file. For example, on my system:
# ./usbsig /dev/bus/usb/001/001 Got signal 10, signo 10 errno 0 code -4
Quoted 6 lines
I don't know exactly what you're looking for, but it should be pretty easy to modify the test program however you want.
If you need to test the compatibility mode specifically, I can do that here -- I'm running a 32-bit userspace on a 64-bit kernel. But you'll have to tell me exactly what test code to run.
Wow I got a little distracted but now I am back to this.
Using your test program I was able to test the basics of this.
I found one bug in my patch where I was missing a memset. So I have corrected that, and reorganized the patch a little bit.
I have not figured out how to trigger a usb disconnect so I have not tested that.
The big thing I have not been able to test is running a 64bit big-endian kernel with a 32bit user space. My modified version of your test program should report "Bad" without my patch, and should report "Good" with it.
Is there any chance you can test that configuration? I could not figure out how to get a 64bit big-endian system running in qemu, and I don't have the necessary hardware so I was not able to test that at all. As that is the actual bug I am still hoping someone can test it.
Thank you,
Eric Biederman
---
/* usbsig.c -- test USB async signal delivery */
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/usb/ch9.h>
#include <linux/usbdevice_fs.h>
static struct usbdevfs_urb urb;
static struct usbdevfs_disconnectsignal ds;
void urb_handler(int sig, siginfo_t *info , void *ucontext)
{
printf("Got signal %d, signo %d errno %d code %d addr: %p urb: %p\n",
sig, info->si_signo, info->si_errno, info->si_code,
info->si_addr, &urb);
printf("%s\n", (info->si_addr == &urb) ? "Good" : "Bad");
}void ds_handler(int sig, siginfo_t *info , void *ucontext)
{
printf("Got signal %d, signo %d errno %d code %d addr: %p ds: %p\n",
sig, info->si_signo, info->si_errno, info->si_code,
info->si_addr, &ds);
printf("%s\n", (info->si_addr == &ds) ? "Good" : "Bad");
}int main(int argc, char **argv)
{
char *devfilename;
int fd;
int rc;
struct sigaction act;
struct usb_ctrlrequest *req;
void *ptr;
char buf[80];
if (argc != 2) {
fprintf(stderr, "Usage: usbsig device-file-name\n");
return 1;
}
devfilename = argv[1];
fd = open(devfilename, O_RDWR);
if (fd == -1) {
perror("Error opening device file");
return 1;
}
act.sa_sigaction = urb_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
rc = sigaction(SIGUSR1, &act, NULL);
if (rc == -1) {
perror("Error in sigaction");
return 1;
}
act.sa_sigaction = ds_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
rc = sigaction(SIGUSR2, &act, NULL);
if (rc == -1) {
perror("Error in sigaction");
return 1;
}
memset(&urb, 0, sizeof(urb));
urb.type = USBDEVFS_URB_TYPE_CONTROL;
urb.endpoint = USB_DIR_IN | 0;
urb.buffer = buf;
urb.buffer_length = sizeof(buf);
urb.signr = SIGUSR1;
req = (struct usb_ctrlrequest *) buf;
req->bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
req->bRequest = USB_REQ_GET_DESCRIPTOR;
req->wValue = USB_DT_DEVICE << 8;
req->wIndex = 0;
req->wLength = sizeof(buf) - sizeof(*req);
rc = ioctl(fd, USBDEVFS_SUBMITURB, &urb);
if (rc == -1) {
perror("Error in SUBMITURB ioctl");
return 1;
}
rc = ioctl(fd, USBDEVFS_REAPURB, &ptr);
if (rc == -1) {
perror("Error in REAPURB ioctl");
return 1;
}
memset(&ds, 0, sizeof(ds));
ds.signr = SIGUSR2;
ds.context = &ds;
rc = ioctl(fd, USBDEVFS_DISCSIGNAL, &ds);
if (rc == -1) {
perror("Error in DISCSIGNAL ioctl");
return 1;
}
close(fd);
return 0;
}From 0045109b65f32f4228072359282d2f8613fc8964 Mon Sep 17 00:00:00 2001
From: "Eric W. Biederman" <[email protected]>
Date: Thu, 7 Feb 2019 19:44:12 -0600
Subject: [PATCH] signal/usb: Replace kill_pid_info_as_cred with
kill_pid_usb_asyncio
The usb support for asyncio encoded one of it's values in the wrong field. It should have used si_value but instead used si_addr which is not present in the _rt union member of struct siginfo.
The result is a POSIX and glibc incompatible encoding of fields in struct siginfo with si_code of SI_ASYNCIO. This makes it impossible to look at a struct siginfo with si_code set to SI_ASYNCIO and without context properly decode it. Which unfortunately means that copy_siginfo_to_user32 can't handle the compat issues this unfortunate choice in encodings brings up.
Therefore replace kill_pid_info_as_cred with kill_pid_usb_asyncio a dedicated function for this one specific case. There are no other users of kill_pid_info_as_cred so this specialization should have no impact on the amount of code in the kernel. Have kill_pid_usb_asyncio take instead of a siginfo_t which is difficult and error prone, 3 arguments, a signal number, an errno value, and an address enconded as a sigval_t. The encoding of the address as a sigval_t allows the caller to deal with the compat issues before calling kill_pid_info_as_cred.
Add BUILD_BUG_ONs in kernel/signal.c to ensure that we can now place the pointer value at the in si_pid (instead of si_addr). That is the code now verifies that si_pid and si_addr always occur at the same location. Further the code veries that for native structures a value placed in si_pid and spilling into si_uid will appear in userspace in si_addr (on a byte by byte copy of siginfo or a field by field copy of siginfo). The code also verifies that for a 64bit kernel and a 32bit userspace the 32bit pointer will fit in si_pid.
The usb code is updated to compute the sigval_t to pass to kill_pid_usb_asyncio when the signals are registered with the usb layer.
--- drivers/usb/core/devio.c | 48 ++++++++++++------------- include/linux/sched/signal.h | 2 +- kernel/signal.c | 69 +++++++++++++++++++++++++++++++----- 3 files changed, 86 insertions(+), 33 deletions(-)
drivers/usb/core/devio.c +24 -24
@@ -63,7 +63,7 @@ struct usb_dev_state {unsigned int discsignr;struct pid *disc_pid;const struct cred *cred; -void __user *disccontext; +sigval_t disccontext;unsigned long ifclaimed;u32 disabled_bulk_eps;bool privileges_dropped;
@@ -90,6 +90,7 @@ struct async {unsigned int ifnum;void __user *userbuffer;void __user *userurb; +sigval_t userurb_sigval;struct urb *urb;struct usb_memory *usbm;unsigned int mem_usage;
@@ -582,22 +583,19 @@ static void async_completed(struct urb *urb) {struct async *as = urb->context;struct usb_dev_state *ps = as->ps; -struct kernel_siginfo sinfo;struct pid *pid =NULL ;const struct cred *cred =NULL ;unsigned long flags; -int signr; +sigval_t addr; +int signr, errno;spin_lock_irqsave (&ps->lock, flags);list_move_tail (&as->asynclist, &ps->async_completed); as->status = urb->status; signr = as->signr;if (signr) { -clear_siginfo (&sinfo); - sinfo.si_signo = as->signr; - sinfo.si_errno = as->status; - sinfo.si_code =SI_ASYNCIO ; - sinfo.si_addr = as->userurb; + errno = as->status; + addr = as->userurb_sigval; pid =get_pid (as->pid); cred =get_cred (as->cred); }
@@ -615,7 +613,7 @@ static void async_completed(struct urb *urb)spin_unlock_irqrestore (&ps->lock, flags);if (signr) { -kill_pid_info_as_cred (sinfo.si_signo, &sinfo, pid, cred); +kill_pid_usb_asyncio (signr, errno, addr, pid, cred);put_pid (pid);put_cred (cred); }
@@ -1427,7 +1425,7 @@ find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)static int proc_do_submiturb (struct usb_dev_state *ps,struct usbdevfs_urb *uurb,struct usbdevfs_iso_packet_desc __user *iso_frame_desc, -void __user *arg) +void __user *arg,sigval_t userurb_sigval) {struct usbdevfs_iso_packet_desc *isopkt =NULL ;struct usb_host_endpoint *ep;
@@ -1727,6 +1725,7 @@ static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb isopkt =NULL ; as->ps = ps; as->userurb = arg; + as->userurb_sigval = userurb_sigval;if (as->usbm) {unsigned long uurb_start = (unsigned long )uurb->buffer;
@@ -1801,13 +1800,17 @@ static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurbstatic int proc_submiturb (struct usb_dev_state *ps,void __user *arg) {struct usbdevfs_urb uurb; +sigval_t userurb_sigval;if (copy_from_user (&uurb, arg,sizeof (uurb)))return -EFAULT ; +memset (&userurb_sigval,0 ,sizeof (userurb_sigval)); + userurb_sigval.sival_ptr = arg; +return proc_do_submiturb (ps, &uurb, (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), - arg); + arg, userurb_sigval); }static int proc_unlinkurb (struct usb_dev_state *ps,void __user *arg)
@@ -1977,7 +1980,7 @@ static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *aif (copy_from_user (&ds, arg,sizeof (ds)))return -EFAULT ; ps->discsignr = ds.signr; - ps->disccontext =compat_ptr (ds.context); + ps->disccontext.sival_int = ds.context;return 0 ; }
@@ -2005,13 +2008,17 @@ static int get_urb32(struct usbdevfs_urb *kurb,static int proc_submiturb_compat (struct usb_dev_state *ps,void __user *arg) {struct usbdevfs_urb uurb; +sigval_t userurb_sigval;if (get_urb32 (&uurb, (struct usbdevfs_urb32 __user *)arg))return -EFAULT ; +memset (&userurb_sigval,0 ,sizeof (userurb_sigval)); + userurb_sigval.sival_int =ptr_to_compat (arg); +return proc_do_submiturb (ps, &uurb, ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, - arg); + arg, userurb_sigval); }static int processcompl_compat (struct async *as,void __user *__user *arg)
@@ -2092,7 +2099,7 @@ static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)if (copy_from_user (&ds, arg,sizeof (ds)))return -EFAULT ; ps->discsignr = ds.signr; - ps->disccontext = ds.context; + ps->disccontext.sival_ptr = ds.context;return 0 ; }
@@ -2614,22 +2621,15 @@ const struct file_operations usbdev_file_operations = {static void usbdev_remove (struct usb_device *udev) {struct usb_dev_state *ps; -struct kernel_siginfo sinfo;while (!list_empty (&udev->filelist)) { ps = list_entry(udev->filelist.next, struct usb_dev_state, list);destroy_all_async (ps);wake_up_all (&ps->wait);list_del_init (&ps->list); -if (ps->discsignr) { -clear_siginfo (&sinfo); - sinfo.si_signo = ps->discsignr; - sinfo.si_errno =EPIPE ; - sinfo.si_code =SI_ASYNCIO ; - sinfo.si_addr = ps->disccontext; -kill_pid_info_as_cred (ps->discsignr, &sinfo, - ps->disc_pid, ps->cred); - } +if (ps->discsignr) +kill_pid_usb_asyncio (ps->discsignr,EPIPE , ps->disccontext, + ps->disc_pid, ps->cred); } }
include/linux/sched/signal.h +1 -1
@@ -328,7 +328,7 @@ extern void force_sigsegv(int sig, struct task_struct *p);extern int force_sig_info (int ,struct kernel_siginfo *,struct task_struct *);extern int __kill_pgrp_info (int sig,struct kernel_siginfo *info,struct pid *pgrp);extern int kill_pid_info (int sig,struct kernel_siginfo *info,struct pid *pid); -extern int kill_pid_info_as_cred (int ,struct kernel_siginfo *,struct pid *, +extern int kill_pid_usb_asyncio (int sig,int errno,sigval_t addr,struct pid *,const struct cred *);extern int kill_pgrp (struct pid *pid,int sig,int priv);extern int kill_pid (struct pid *pid,int sig,int priv);
kernel/signal.c +61 -8
104 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 9 lines · Eric W. Biederman
Wow I got a little distracted but now I am back to this.
Using your test program I was able to test the basics of this.
I found one bug in my patch where I was missing a memset. So I have corrected that, and reorganized the patch a little bit.
I have not figured out how to trigger a usb disconnect so I have not tested that.
Heh. Assuming the device file you tell the test program to use corresponds to an actual USB device, you can trigger a disconnect by literally unplugging the USB cable. (Add a 10-second delay to the program to give yourself enough time.)
Quoted 9 lines
The big thing I have not been able to test is running a 64bit big-endian kernel with a 32bit user space. My modified version of your test program should report "Bad" without my patch, and should report "Good" with it.
Is there any chance you can test that configuration? I could not figure out how to get a 64bit big-endian system running in qemu, and I don't have the necessary hardware so I was not able to test that at all. As that is the actual bug I am still hoping someone can test it.
Unfortunately, I don't have any big-endian systems either.
Alan Stern
Quoted 9 lines · Alan Stern
Quoted 7 lines · Eric W. Biederman
Wow I got a little distracted but now I am back to this.
Using your test program I was able to test the basics of this.
I found one bug in my patch where I was missing a memset. So I have corrected that, and reorganized the patch a little bit.
Quoted 7 lines
Quoted 2 lines
I have not figured out how to trigger a usb disconnect so I have not tested that.
Heh. Assuming the device file you tell the test program to use corresponds to an actual USB device, you can trigger a disconnect by literally unplugging the USB cable. (Add a 10-second delay to the program to give yourself enough time.)
I have just been running this in qemu. But yes. I suppose the easy way would be to print a message asking the usb device to be unplugged and then just wait for the signal. I might try that.
Quoted 11 lines
Quoted 9 lines
The big thing I have not been able to test is running a 64bit big-endian kernel with a 32bit user space. My modified version of your test program should report "Bad" without my patch, and should report "Good" with it.
Is there any chance you can test that configuration? I could not figure out how to get a 64bit big-endian system running in qemu, and I don't have the necessary hardware so I was not able to test that at all. As that is the actual bug I am still hoping someone can test it.
Unfortunately, I don't have any big-endian systems either.
That probably explains why the breakage in big-endian was never noticed. I am starting to wonder if anyone is actually doing big-endian for new systems anymore.
Eric
PATCH 1/1 signal/usb: Replace kill_pid_info_as_cred with kill_pid_usb_asyncio
In reply to Alan SternThe usb support for asyncio encoded one of it's values in the wrong field. It should have used si_value but instead used si_addr which is not present in the _rt union member of struct siginfo.
The practical result of this is that on a 64bit big endian kernel when delivering a signal to a 32bit process the si_addr field is set to NULL, instead of the expected pointer value.
This issue can not be fixed in copy_siginfo_to_user32 as the usb usage of the the _sigfault (aka si_addr) member of the siginfo union when SI_ASYNCIO is set is incompatible with the POSIX and glibc usage of the _rt member of the siginfo union.
Therefore replace kill_pid_info_as_cred with kill_pid_usb_asyncio a dedicated function for this one specific case. There are no other users of kill_pid_info_as_cred so this specialization should have no impact on the amount of code in the kernel. Have kill_pid_usb_asyncio take instead of a siginfo_t which is difficult and error prone, 3 arguments, a signal number, an errno value, and an address enconded as a sigval_t. The encoding of the address as a sigval_t allows the code that reads the userspace request for a signal to handle this compat issue along with all of the other compat issues.
Add BUILD_BUG_ONs in kernel/signal.c to ensure that we can now place the pointer value at the in si_pid (instead of si_addr). That is the code now verifies that si_pid and si_addr always occur at the same location. Further the code veries that for native structures a value placed in si_pid and spilling into si_uid will appear in userspace in si_addr (on a byte by byte copy of siginfo or a field by field copy of siginfo). The code also verifies that for a 64bit kernel and a 32bit userspace the 32bit pointer will fit in si_pid.
I have used the usbsig.c program below written by Alan Stern and slightly tweaked by me to run on a big endian machine to verify the issue exists (on sparc64) and to confirm the patch below fixes the issue.
/* usbsig.c -- test USB async signal delivery */
static struct usbdevfs_urb urb;
static struct usbdevfs_disconnectsignal ds;
static volatile sig_atomic_t done = 0;
void urb_handler(int sig, siginfo_t *info , void *ucontext)
{
printf("Got signal %d, signo %d errno %d code %d addr: %p urb: %p\n",
sig, info->si_signo, info->si_errno, info->si_code,
info->si_addr, &urb);
printf("%s\n", (info->si_addr == &urb) ? "Good" : "Bad");
}void ds_handler(int sig, siginfo_t *info , void *ucontext)
{
printf("Got signal %d, signo %d errno %d code %d addr: %p ds: %p\n",
sig, info->si_signo, info->si_errno, info->si_code,
info->si_addr, &ds);
printf("%s\n", (info->si_addr == &ds) ? "Good" : "Bad");
done = 1;
}int main(int argc, char **argv)
{
char *devfilename;
int fd;
int rc;
struct sigaction act;
struct usb_ctrlrequest *req;
void *ptr;
char buf[80];
if (argc != 2) {
fprintf(stderr, "Usage: usbsig device-file-name\n");
return 1;
}
devfilename = argv[1];
fd = open(devfilename, O_RDWR);
if (fd == -1) {
perror("Error opening device file");
return 1;
}
act.sa_sigaction = urb_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
rc = sigaction(SIGUSR1, &act, NULL);
if (rc == -1) {
perror("Error in sigaction");
return 1;
}
act.sa_sigaction = ds_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
rc = sigaction(SIGUSR2, &act, NULL);
if (rc == -1) {
perror("Error in sigaction");
return 1;
}
memset(&urb, 0, sizeof(urb));
urb.type = USBDEVFS_URB_TYPE_CONTROL;
urb.endpoint = USB_DIR_IN | 0;
urb.buffer = buf;
urb.buffer_length = sizeof(buf);
urb.signr = SIGUSR1;
req = (struct usb_ctrlrequest *) buf;
req->bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
req->bRequest = USB_REQ_GET_DESCRIPTOR;
req->wValue = htole16(USB_DT_DEVICE << 8);
req->wIndex = htole16(0);
req->wLength = htole16(sizeof(buf) - sizeof(*req));
rc = ioctl(fd, USBDEVFS_SUBMITURB, &urb);
if (rc == -1) {
perror("Error in SUBMITURB ioctl");
return 1;
}
rc = ioctl(fd, USBDEVFS_REAPURB, &ptr);
if (rc == -1) {
perror("Error in REAPURB ioctl");
return 1;
}
memset(&ds, 0, sizeof(ds));
ds.signr = SIGUSR2;
ds.context = &ds;
rc = ioctl(fd, USBDEVFS_DISCSIGNAL, &ds);
if (rc == -1) {
perror("Error in DISCSIGNAL ioctl");
return 1;
}
printf("Waiting for usb disconnect\n");
while (!done) {
sleep(1);
}
close(fd);
return 0;
}---
I managed to wrestle a sparc64 qemu to the ground so I could verify this bug exists and the patch below fixes it.
Can I get an Ack from the usb side of things?
I intend to merge this through my siginfo tree unless someone objects.
Thank you,
Eric Biederman
drivers/usb/core/devio.c | 48 ++++++++++++------------- include/linux/sched/signal.h | 2 +- kernel/signal.c | 69 +++++++++++++++++++++++++++++++----- 3 files changed, 86 insertions(+), 33 deletions(-)
drivers/usb/core/devio.c +24 -24
@@ -63,7 +63,7 @@ struct usb_dev_state {unsigned int discsignr;struct pid *disc_pid;const struct cred *cred; -void __user *disccontext; +sigval_t disccontext;unsigned long ifclaimed;u32 disabled_bulk_eps;bool privileges_dropped;
@@ -90,6 +90,7 @@ struct async {unsigned int ifnum;void __user *userbuffer;void __user *userurb; +sigval_t userurb_sigval;struct urb *urb;struct usb_memory *usbm;unsigned int mem_usage;
@@ -582,22 +583,19 @@ static void async_completed(struct urb *urb) {struct async *as = urb->context;struct usb_dev_state *ps = as->ps; -struct kernel_siginfo sinfo;struct pid *pid =NULL ;const struct cred *cred =NULL ;unsigned long flags; -int signr; +sigval_t addr; +int signr, errno;spin_lock_irqsave (&ps->lock, flags);list_move_tail (&as->asynclist, &ps->async_completed); as->status = urb->status; signr = as->signr;if (signr) { -clear_siginfo (&sinfo); - sinfo.si_signo = as->signr; - sinfo.si_errno = as->status; - sinfo.si_code =SI_ASYNCIO ; - sinfo.si_addr = as->userurb; + errno = as->status; + addr = as->userurb_sigval; pid =get_pid (as->pid); cred =get_cred (as->cred); }
@@ -615,7 +613,7 @@ static void async_completed(struct urb *urb)spin_unlock_irqrestore (&ps->lock, flags);if (signr) { -kill_pid_info_as_cred (sinfo.si_signo, &sinfo, pid, cred); +kill_pid_usb_asyncio (signr, errno, addr, pid, cred);put_pid (pid);put_cred (cred); }
@@ -1427,7 +1425,7 @@ find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)static int proc_do_submiturb (struct usb_dev_state *ps,struct usbdevfs_urb *uurb,struct usbdevfs_iso_packet_desc __user *iso_frame_desc, -void __user *arg) +void __user *arg,sigval_t userurb_sigval) {struct usbdevfs_iso_packet_desc *isopkt =NULL ;struct usb_host_endpoint *ep;
@@ -1727,6 +1725,7 @@ static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb isopkt =NULL ; as->ps = ps; as->userurb = arg; + as->userurb_sigval = userurb_sigval;if (as->usbm) {unsigned long uurb_start = (unsigned long )uurb->buffer;
@@ -1801,13 +1800,17 @@ static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurbstatic int proc_submiturb (struct usb_dev_state *ps,void __user *arg) {struct usbdevfs_urb uurb; +sigval_t userurb_sigval;if (copy_from_user (&uurb, arg,sizeof (uurb)))return -EFAULT ; +memset (&userurb_sigval,0 ,sizeof (userurb_sigval)); + userurb_sigval.sival_ptr = arg; +return proc_do_submiturb (ps, &uurb, (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), - arg); + arg, userurb_sigval); }static int proc_unlinkurb (struct usb_dev_state *ps,void __user *arg)
@@ -1977,7 +1980,7 @@ static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *aif (copy_from_user (&ds, arg,sizeof (ds)))return -EFAULT ; ps->discsignr = ds.signr; - ps->disccontext =compat_ptr (ds.context); + ps->disccontext.sival_int = ds.context;return 0 ; }
@@ -2005,13 +2008,17 @@ static int get_urb32(struct usbdevfs_urb *kurb,static int proc_submiturb_compat (struct usb_dev_state *ps,void __user *arg) {struct usbdevfs_urb uurb; +sigval_t userurb_sigval;if (get_urb32 (&uurb, (struct usbdevfs_urb32 __user *)arg))return -EFAULT ; +memset (&userurb_sigval,0 ,sizeof (userurb_sigval)); + userurb_sigval.sival_int =ptr_to_compat (arg); +return proc_do_submiturb (ps, &uurb, ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, - arg); + arg, userurb_sigval); }static int processcompl_compat (struct async *as,void __user *__user *arg)
@@ -2092,7 +2099,7 @@ static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)if (copy_from_user (&ds, arg,sizeof (ds)))return -EFAULT ; ps->discsignr = ds.signr; - ps->disccontext = ds.context; + ps->disccontext.sival_ptr = ds.context;return 0 ; }
@@ -2614,22 +2621,15 @@ const struct file_operations usbdev_file_operations = {static void usbdev_remove (struct usb_device *udev) {struct usb_dev_state *ps; -struct kernel_siginfo sinfo;while (!list_empty (&udev->filelist)) { ps = list_entry(udev->filelist.next, struct usb_dev_state, list);destroy_all_async (ps);wake_up_all (&ps->wait);list_del_init (&ps->list); -if (ps->discsignr) { -clear_siginfo (&sinfo); - sinfo.si_signo = ps->discsignr; - sinfo.si_errno =EPIPE ; - sinfo.si_code =SI_ASYNCIO ; - sinfo.si_addr = ps->disccontext; -kill_pid_info_as_cred (ps->discsignr, &sinfo, - ps->disc_pid, ps->cred); - } +if (ps->discsignr) +kill_pid_usb_asyncio (ps->discsignr,EPIPE , ps->disccontext, + ps->disc_pid, ps->cred); } }
include/linux/sched/signal.h +1 -1
@@ -329,7 +329,7 @@ extern void force_sigsegv(int sig, struct task_struct *p);extern int force_sig_info (int ,struct kernel_siginfo *,struct task_struct *);extern int __kill_pgrp_info (int sig,struct kernel_siginfo *info,struct pid *pgrp);extern int kill_pid_info (int sig,struct kernel_siginfo *info,struct pid *pid); -extern int kill_pid_info_as_cred (int ,struct kernel_siginfo *,struct pid *, +extern int kill_pid_usb_asyncio (int sig,int errno,sigval_t addr,struct pid *,const struct cred *);extern int kill_pgrp (struct pid *pid,int sig,int priv);extern int kill_pid (struct pid *pid,int sig,int priv);
kernel/signal.c +61 -8
104 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 108 lines · Eric W. Biederman
The usb support for asyncio encoded one of it's values in the wrong field. It should have used
si_valuebut instead usedsi_addrwhich is not present in the_rtunion member of struct siginfo.The practical result of this is that on a 64bit big endian kernel when delivering a signal to a 32bit process the
si_addrfield is set to NULL, instead of the expected pointer value.This issue can not be fixed in
copy_siginfo_to_user32as the usb usage of the the_sigfault(akasi_addr) member of the siginfo union whenSI_ASYNCIOis set is incompatible with the POSIX and glibc usage of the_rtmember of the siginfo union.Therefore replace
kill_pid_info_as_credwithkill_pid_usb_asyncioa dedicated function for this one specific case. There are no other users ofkill_pid_info_as_credso this specialization should have no impact on the amount of code in the kernel. Havekill_pid_usb_asynciotake instead of asiginfo_twhich is difficult and error prone, 3 arguments, a signal number, an errno value, and an address enconded as asigval_t. The encoding of the address as asigval_tallows the code that reads the userspace request for a signal to handle this compat issue along with all of the other compat issues.Add
BUILD_BUG_ONsinkernel/signal.cto ensure that we can now place the pointer value at the insi_pid(instead ofsi_addr). That is the code now verifies thatsi_pidandsi_addralways occur at the same location. Further the code veries that for native structures a value placed insi_pidand spilling intosi_uidwill appear in userspace insi_addr(on a byte by byte copy of siginfo or a field by field copy of siginfo). The code also verifies that for a 64bit kernel and a 32bit userspace the 32bit pointer will fit insi_pid.I have used the
usbsig.cprogram below written by Alan Stern and slightly tweaked by me to run on a big endian machine to verify the issue exists (on sparc64) and to confirm the patch below fixes the issue./*
usbsig.c-- test USB async signal delivery */static struct
usbdevfs_urburb;
static structusbdevfs_disconnectsignalds;
static volatilesig_atomic_tdone = 0;void
urb_handler(int sig, siginfo_t *info , void *ucontext){ printf("Got signal %d, signo %d errno %d code %d addr: %p urb: %p\n", sig, info->si_signo, info->si_errno, info->si_code, info->si_addr, &urb); printf("%s\n", (info->si_addr == &urb) ? "Good" : "Bad"); }void
ds_handler(int sig, siginfo_t *info , void *ucontext){ printf("Got signal %d, signo %d errno %d code %d addr: %p ds: %p\n", sig, info->si_signo, info->si_errno, info->si_code, info->si_addr, &ds); printf("%s\n", (info->si_addr == &ds) ? "Good" : "Bad"); done = 1; }int
main(int argc, char **argv){ char *devfilename; int fd; int rc; struct sigaction act; struct usb_ctrlrequest *req; void *ptr; char buf[80]; if (argc != 2) { fprintf(stderr, "Usage: usbsig device-file-name\n"); return 1; } devfilename = argv[1]; fd = open(devfilename, O_RDWR); if (fd == -1) { perror("Error opening device file"); return 1; } act.sa_sigaction = urb_handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_SIGINFO; rc = sigaction(SIGUSR1, &act, NULL); if (rc == -1) { perror("Error in sigaction"); return 1; } act.sa_sigaction = ds_handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_SIGINFO; rc = sigaction(SIGUSR2, &act, NULL); if (rc == -1) { perror("Error in sigaction"); return 1; } memset(&urb, 0, sizeof(urb)); urb.type = USBDEVFS_URB_TYPE_CONTROL; urb.endpoint = USB_DIR_IN | 0; urb.buffer = buf; urb.buffer_length = sizeof(buf);
Quoted 8 lines
urb.signr = SIGUSR1; req = (struct usb_ctrlrequest *) buf; req->bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE; req->bRequest = USB_REQ_GET_DESCRIPTOR; req->wValue = htole16(USB_DT_DEVICE << 8); req->wIndex = htole16(0); req->wLength = htole16(sizeof(buf) - sizeof(*req));
In fact, these values are supposed to be in host-endian order, not necessarily little-endian. The USB core converts them if necessary.
Quoted 37 lines
rc = ioctl(fd, USBDEVFS_SUBMITURB, &urb); if (rc == -1) { perror("Error in SUBMITURB ioctl"); return 1; } rc = ioctl(fd, USBDEVFS_REAPURB, &ptr); if (rc == -1) { perror("Error in REAPURB ioctl"); return 1; } memset(&ds, 0, sizeof(ds)); ds.signr = SIGUSR2; ds.context = &ds; rc = ioctl(fd, USBDEVFS_DISCSIGNAL, &ds); if (rc == -1) { perror("Error in DISCSIGNAL ioctl"); return 1; } printf("Waiting for usb disconnect\n"); while (!done) { sleep(1); } close(fd); return 0; }Cc Greg Kroah-Hartman <[email protected]>Cc Alan Stern <[email protected]>Cc Oliver Neukum <[email protected]>Fixes v2.3.39Signed-off-by "Eric W. Biederman" <[email protected]>
Quoted 6 lines
---
I managed to wrestle a sparc64 qemu to the ground so I could verify this bug exists and the patch below fixes it.
Can I get an Ack from the usb side of things?
Give me some time to review the description and the changes.
Alan Stern
Quoted 34 lines · Alan Stern
Quoted 32 lines · Eric W. Biederman
The usb support for asyncio encoded one of it's values in the wrong field. It should have used
si_valuebut instead usedsi_addrwhich is not present in the_rtunion member of struct siginfo.The practical result of this is that on a 64bit big endian kernel when delivering a signal to a 32bit process the
si_addrfield is set to NULL, instead of the expected pointer value.This issue can not be fixed in
copy_siginfo_to_user32as the usb usage of the the_sigfault(akasi_addr) member of the siginfo union whenSI_ASYNCIOis set is incompatible with the POSIX and glibc usage of the_rtmember of the siginfo union.Therefore replace
kill_pid_info_as_credwithkill_pid_usb_asyncioa dedicated function for this one specific case. There are no other users ofkill_pid_info_as_credso this specialization should have no impact on the amount of code in the kernel. Havekill_pid_usb_asynciotake instead of asiginfo_twhich is difficult and error prone, 3 arguments, a signal number, an errno value, and an address enconded as asigval_t. The encoding of the address as asigval_tallows the code that reads the userspace request for a signal to handle this compat issue along with all of the other compat issues.Add
BUILD_BUG_ONsinkernel/signal.cto ensure that we can now place the pointer value at the insi_pid(instead ofsi_addr). That is the code now verifies thatsi_pidandsi_addralways occur at the same location. Further the code veries that for native structures a value placed insi_pidand spilling intosi_uidwill appear in userspace insi_addr(on a byte by byte copy of siginfo or a field by field copy of siginfo). The code also verifies that for a 64bit kernel and a 32bit userspace the 32bit pointer will fit insi_pid.
Quoted 5 lines
Quoted 5 lines
I have used the
usbsig.cprogram below written by Alan Stern and slightly tweaked by me to run on a big endian machine to verify the issue exists (on sparc64) and to confirm the patch below fixes the issue./*
usbsig.c-- test USB async signal delivery */
Sigh git commit ate the includes...
Quoted 79 lines
Quoted 70 lines
static struct
usbdevfs_urburb;
static structusbdevfs_disconnectsignalds;
static volatilesig_atomic_tdone = 0;void
urb_handler(int sig, siginfo_t *info , void *ucontext){ printf("Got signal %d, signo %d errno %d code %d addr: %p urb: %p\n", sig, info->si_signo, info->si_errno, info->si_code, info->si_addr, &urb); printf("%s\n", (info->si_addr == &urb) ? "Good" : "Bad"); }void
ds_handler(int sig, siginfo_t *info , void *ucontext){ printf("Got signal %d, signo %d errno %d code %d addr: %p ds: %p\n", sig, info->si_signo, info->si_errno, info->si_code, info->si_addr, &ds); printf("%s\n", (info->si_addr == &ds) ? "Good" : "Bad"); done = 1; }int
main(int argc, char **argv){ char *devfilename; int fd; int rc; struct sigaction act; struct usb_ctrlrequest *req; void *ptr; char buf[80]; if (argc != 2) { fprintf(stderr, "Usage: usbsig device-file-name\n"); return 1; } devfilename = argv[1]; fd = open(devfilename, O_RDWR); if (fd == -1) { perror("Error opening device file"); return 1; } act.sa_sigaction = urb_handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_SIGINFO; rc = sigaction(SIGUSR1, &act, NULL); if (rc == -1) { perror("Error in sigaction"); return 1; } act.sa_sigaction = ds_handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_SIGINFO; rc = sigaction(SIGUSR2, &act, NULL); if (rc == -1) { perror("Error in sigaction"); return 1; } memset(&urb, 0, sizeof(urb)); urb.type = USBDEVFS_URB_TYPE_CONTROL; urb.endpoint = USB_DIR_IN | 0; urb.buffer = buf; urb.buffer_length = sizeof(buf);Quoted 8 lines
urb.signr = SIGUSR1; req = (struct usb_ctrlrequest *) buf; req->bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE; req->bRequest = USB_REQ_GET_DESCRIPTOR; req->wValue = htole16(USB_DT_DEVICE << 8); req->wIndex = htole16(0); req->wLength = htole16(sizeof(buf) - sizeof(*req));
Quoted 2 lines
In fact, these values are supposed to be in host-endian order, not necessarily little-endian. The USB core converts them if necessary.
Please look again. In include/uapi/linux/ch9.h those fields are explicitly defined as little endian and the code in devio.c forUSBDEVFS_URB_TYPE_CONTROL treats them as little endian. Perhaps there
is a mismatch here but I haven't seen it and I needed this change to get the code to work on big endian.
Quoted 39 lines
Quoted 39 lines
rc = ioctl(fd, USBDEVFS_SUBMITURB, &urb); if (rc == -1) { perror("Error in SUBMITURB ioctl"); return 1; } rc = ioctl(fd, USBDEVFS_REAPURB, &ptr); if (rc == -1) { perror("Error in REAPURB ioctl"); return 1; } memset(&ds, 0, sizeof(ds)); ds.signr = SIGUSR2; ds.context = &ds; rc = ioctl(fd, USBDEVFS_DISCSIGNAL, &ds); if (rc == -1) { perror("Error in DISCSIGNAL ioctl"); return 1; } printf("Waiting for usb disconnect\n"); while (!done) { sleep(1); } close(fd); return 0; }Cc Greg Kroah-Hartman <[email protected]>Cc Alan Stern <[email protected]>Cc Oliver Neukum <[email protected]>Fixes v2.3.39Signed-off-by "Eric W. Biederman" <[email protected]>---
Quoted 6 lines
Quoted 4 lines
I managed to wrestle a sparc64 qemu to the ground so I could verify this bug exists and the patch below fixes it.
Can I get an Ack from the usb side of things?
Give me some time to review the description and the changes.
Please, it always helps when more people understand these things.
Eric
Quoted 9 lines · Eric W. Biederman
Quoted 7 lines · Alan Stern
Quoted 6 lines
req = (struct usb_ctrlrequest *) buf; req->bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE; req->bRequest = USB_REQ_GET_DESCRIPTOR; req->wValue = htole16(USB_DT_DEVICE << 8); req->wIndex = htole16(0); req->wLength = htole16(sizeof(buf) - sizeof(*req));
Quoted 8 lines
Quoted 2 lines
In fact, these values are supposed to be in host-endian order, not necessarily little-endian. The USB core converts them if necessary.
Please look again. In
include/uapi/linux/ch9.hthose fields are explicitly defined as little endian and the code indevio.cforUSBDEVFS_URB_TYPE_CONTROLtreats them as little endian. Perhaps there
is a mismatch here but I haven't seen it and I needed this change to get the code to work on big endian.
Oops -- you're right. I was thinking of the USBDEVFS_CONTROL ioctl. Sorry for the mistake.
Alan Stern
Quoted 23 lines · Eric W. Biederman
The usb support for asyncio encoded one of it's values in the wrong field. It should have used
si_valuebut instead usedsi_addrwhich is not present in the_rtunion member of struct siginfo.The practical result of this is that on a 64bit big endian kernel when delivering a signal to a 32bit process the
si_addrfield is set to NULL, instead of the expected pointer value.This issue can not be fixed in
copy_siginfo_to_user32as the usb usage of the the_sigfault(akasi_addr) member of the siginfo union whenSI_ASYNCIOis set is incompatible with the POSIX and glibc usage of the_rtmember of the siginfo union.Therefore replace
kill_pid_info_as_credwithkill_pid_usb_asyncioa dedicated function for this one specific case. There are no other users ofkill_pid_info_as_credso this specialization should have no impact on the amount of code in the kernel. Havekill_pid_usb_asynciotake instead of asiginfo_twhich is difficult and error prone, 3 arguments, a signal number, an errno value, and an address enconded as asigval_t. The encoding of the address as asigval_tallows the code that reads the userspace request for a signal to handle this compat issue along with all of the other compat issues.
Quoted 8 lines
Add
BUILD_BUG_ONsinkernel/signal.cto ensure that we can now place the pointer value at the insi_pid(instead ofsi_addr). That is the code now verifies thatsi_pidandsi_addralways occur at the same location. Further the code veries that for native structures a value placed insi_pidand spilling intosi_uidwill appear in userspace insi_addr(on a byte by byte copy of siginfo or a field by field copy of siginfo). The code also verifies that for a 64bit kernel and a 32bit userspace the 32bit pointer will fit insi_pid.
Okay, I have gone through this. Although I still don't really understand the detailed issues concerning the layout of the data fields (probably hopeless without seeing a diagram), the USB portions of the patch look good and do what the patch description says.
Alan Stern
Quoted 34 lines · Alan Stern
Quoted 23 lines · Eric W. Biederman
The usb support for asyncio encoded one of it's values in the wrong field. It should have used
si_valuebut instead usedsi_addrwhich is not present in the_rtunion member of struct siginfo.The practical result of this is that on a 64bit big endian kernel when delivering a signal to a 32bit process the
si_addrfield is set to NULL, instead of the expected pointer value.This issue can not be fixed in
copy_siginfo_to_user32as the usb usage of the the_sigfault(akasi_addr) member of the siginfo union whenSI_ASYNCIOis set is incompatible with the POSIX and glibc usage of the_rtmember of the siginfo union.Therefore replace
kill_pid_info_as_credwithkill_pid_usb_asyncioa dedicated function for this one specific case. There are no other users ofkill_pid_info_as_credso this specialization should have no impact on the amount of code in the kernel. Havekill_pid_usb_asynciotake instead of asiginfo_twhich is difficult and error prone, 3 arguments, a signal number, an errno value, and an address enconded as asigval_t. The encoding of the address as asigval_tallows the code that reads the userspace request for a signal to handle this compat issue along with all of the other compat issues.Quoted 8 lines
Add
BUILD_BUG_ONsinkernel/signal.cto ensure that we can now place the pointer value at the insi_pid(instead ofsi_addr). That is the code now verifies thatsi_pidandsi_addralways occur at the same location. Further the code veries that for native structures a value placed insi_pidand spilling intosi_uidwill appear in userspace insi_addr(on a byte by byte copy of siginfo or a field by field copy of siginfo). The code also verifies that for a 64bit kernel and a 32bit userspace the 32bit pointer will fit insi_pid.
Quoted 8 lines
Okay, I have gone through this. Although I still don't really understand the detailed issues concerning the layout of the data fields (probably hopeless without seeing a diagram), the USB portions of the patch look good and do what the patch description says.
Acked-by Alan Stern <[email protected]>Alan Stern
Thanks.
Perhaps this will work as a diagram. I don't know if there is a better way to say it in my patch description. In struct siginfo there are 3 fields in fixed positions:
int si_signo;
int si_errno;
int si_code;
After that there is a union. The si_signo and si_code fields are examined to see which union member is valid (see siginfo_layout). In every other case a si_code of SI_ASYNCIO corresponds to the the _rt union member which has the fields:
int si_pid;
int si_uid;
sigval_t si_sigval;
However when usb started using SI_ASYNCIO the _sigfault union member that (except for special exceptions) only has the field:
void __user *si_addr;
Or in short the relevant piece of the union looks like:
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| si_pid | si_uid |
+---+---+---+---+---+---+---+---+
| si_addr | (64bit)
+---+---+---+---+---+---+---+---+
| si_addr | (32bit)
+---+---+---+---+Which means if siginfo is copied field by field on 32bit everything works because si_pid and si_addr are in the same location.
Similarly if siginfo is copied field by field on 64bit everything works because there is no padding between si_pid and si_uid. So copying both of those fields results in the entire si_addr being copied.
It is the compat case that gets tricky. Half of the bits are zero. If those zero bits show up in bytes 4-7 and the data shows up in bytes 0-3 (aka little endian) everything works. If those zero bits show in in bytes 0-3 (aka big endian) userspace sees a NULL pointer instead of the value it passed.
Fixing this while maintaining some modicum of sanity is the tricky bit. The interface is made to kill_pid_usb_asyncio is made a sigval_t so the standard signal compat tricks can be used. sigval_t is a union of:
int sival_int;
void __user *sival_ptr;
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| sival_ptr | (64bit)
+---+---+---+---+---+---+---+---+
| sival_ptr | (32bit)
+---+---+---+---+
| sival_int |
+---+---+---+---+The signal code solves the compat issues for sigval_t by storing the 32bit pointers in sival_int. So they meaningful bits are guaranteed to be in the low 32bits, just like the 32bit sival_ptr.
After a bunch of build BUG_ONs to verify my reasonable assumptions of but the siginfo layout are actually true, the code that generates the siginfo just copies a sigval_t to si_pid. And assumes the code in the usb stack placed the pointer in the proper part of the sigval_t when it read the information from userspace.
I don't know if that helps make it easy to understand but I figured I would give it a shot.
Eric
Quoted 8 lines · Eric W. Biederman
Perhaps this will work as a diagram. I don't know if there is a better way to say it in my patch description. In struct siginfo there are 3 fields in fixed positions:
int si_signo; int si_errno; int si_code;
Quoted 8 lines
After that there is a union. The
si_signoandsi_codefields are examined to see which union member is valid (seesiginfo_layout). In every other case asi_codeofSI_ASYNCIOcorresponds to the the_rtunion member which has the fields:int si_pid; int si_uid; sigval_t si_sigval;
Yuu mean it's actually a union of structures containing these fields, not a union of these fields, right?
Quoted 16 lines
However when usb started using
SI_ASYNCIOthe_sigfaultunion member that (except for special exceptions) only has the field:void __user *si_addr;Or in short the relevant piece of the union looks like:
0 1 2 3 4 5 6 7 +---+---+---+---+---+---+---+---+ | si_pid | si_uid | +---+---+---+---+---+---+---+---+ | si_addr | (64bit) +---+---+---+---+---+---+---+---+ | si_addr | (32bit) +---+---+---+---+
Quoted 2 lines
Which means if siginfo is copied field by field on 32bit everything works because
si_pidandsi_addrare in the same location.
When you say "copied field by field", you mean that the si_pid, si_uid, and si_sigval values are copied individually?
Quoted 10 lines
Similarly if siginfo is copied field by field on 64bit everything works because there is no padding between
si_pidandsi_uid. So copying both of those fields results in the entiresi_addrbeing copied.It is the compat case that gets tricky. Half of the bits are zero. If those zero bits show up in bytes 4-7 and the data shows up in bytes 0-3 (aka little endian) everything works. If those zero bits show in in bytes 0-3 (aka big endian) userspace sees a NULL pointer instead of the value it passed.
The problem is that the compat translation layer copies si_pid and si_uid from the 64-bit kernel structure to the 32-bit user structure. And since the system is big-endian, the 64-bit si_addr value has zeros in bytes 0-3. But those zeros are what userspace ends up seeing in its 32-bit version of si_addr.
So the solution is to store the address in the si_sigval part instead. Wouldn't it have been easier to have a compat routine somewhere just do something like:
sinfo->si_pid = (u32) sinfo->si_addr; /* Compensate for USB */
That would work regardless of the endianness, wouldn't it?
Quoted 20 lines
Fixing this while maintaining some modicum of sanity is the tricky bit. The interface is made to
kill_pid_usb_asynciois made asigval_tso the standard signal compat tricks can be used.sigval_tis a union of:int sival_int; void __user *sival_ptr; 0 1 2 3 4 5 6 7 +---+---+---+---+---+---+---+---+ | sival_ptr | (64bit) +---+---+---+---+---+---+---+---+ | sival_ptr | (32bit) +---+---+---+---+ | sival_int | +---+---+---+---+The signal code solves the compat issues for
sigval_tby storing the 32bit pointers insival_int. So they meaningful bits are guaranteed to be in the low 32bits, just like the 32bitsival_ptr.
Quoted 8 lines
After a bunch of build
BUG_ONsto verify my reasonable assumptions of but the siginfo layout are actually true, the code that generates the siginfo just copies asigval_ttosi_pid. And assumes the code in the usb stack placed the pointer in the proper part of thesigval_twhen it read the information from userspace.I don't know if that helps make it easy to understand but I figured I would give it a shot.
I think I understand now. Thanks.
Alan
Quoted 15 lines · Alan Stern
Quoted 13 lines · Eric W. Biederman
Perhaps this will work as a diagram. I don't know if there is a better way to say it in my patch description. In struct siginfo there are 3 fields in fixed positions:
int si_signo; int si_errno; int si_code;After that there is a union. The
si_signoandsi_codefields are examined to see which union member is valid (seesiginfo_layout). In every other case asi_codeofSI_ASYNCIOcorresponds to the the_rtunion member which has the fields:
Quoted 6 lines
Quoted 3 lines
int si_pid; int si_uid; sigval_t si_sigval;Yuu mean it's actually a union of structures containing these fields, not a union of these fields, right?
Correct.
Quoted 16 lines
Quoted 16 lines
However when usb started using
SI_ASYNCIOthe_sigfaultunion member that (except for special exceptions) only has the field:void __user *si_addr;Or in short the relevant piece of the union looks like:
0 1 2 3 4 5 6 7 +---+---+---+---+---+---+---+---+ | si_pid | si_uid | +---+---+---+---+---+---+---+---+ | si_addr | (64bit) +---+---+---+---+---+---+---+---+ | si_addr | (32bit) +---+---+---+---+
Quoted 5 lines
Quoted 2 lines
Which means if siginfo is copied field by field on 32bit everything works because
si_pidandsi_addrare in the same location.When you say "copied field by field", you mean that the
si_pid,si_uid, andsi_sigvalvalues are copied individually?
At least historically. I have cleaned things up so no on native we can do a byte by byte copy. But for the compat case we still copy field by field so that we have a chance of translating the fields.
Quoted 17 lines
Quoted 10 lines
Similarly if siginfo is copied field by field on 64bit everything works because there is no padding between
si_pidandsi_uid. So copying both of those fields results in the entiresi_addrbeing copied.It is the compat case that gets tricky. Half of the bits are zero. If those zero bits show up in bytes 4-7 and the data shows up in bytes 0-3 (aka little endian) everything works. If those zero bits show in in bytes 0-3 (aka big endian) userspace sees a NULL pointer instead of the value it passed.
The problem is that the compat translation layer copies
si_pidandsi_uidfrom the 64-bit kernel structure to the 32-bit user structure. And since the system is big-endian, the 64-bitsi_addrvalue has zeros in bytes 0-3. But those zeros are what userspace ends up seeing in its 32-bit version ofsi_addr.
Quoted 7 lines
So the solution is to store the address in the
si_sigvalpart instead. Wouldn't it have been easier to have a compat routine somewhere just do something like:sinfo->si_pid = (u32) sinfo->si_addr; /* Compensate for USB */That would work regardless of the endianness, wouldn't it?
That logic will work yes. That is essentially what I implemented in proc_submiturb_compat:
userurb_sigval.sival_int = ptr_to_compat(arg);
The practical problem is that we only know the compat status of userspace when (a) the usb ioctl is called and when (b) the signal is delivered to userspace. We can't fix this a (b) because there is too little information available due to the fact that glibc also uses SI_ASYNCIO but with si_uid, si_pid, and si_siginfo. So we can't just replace si_pid with sinfo->si_addr because si_pid and si_uid are valid.
So we have to do something at (a) which is complicated by the fact that we don't have siginfo yet. So I choose to store a sigval_t instead to just get the relevant bits.
Quoted 26 lines
Quoted 26 lines
Fixing this while maintaining some modicum of sanity is the tricky bit. The interface is made to
kill_pid_usb_asynciois made asigval_tso the standard signal compat tricks can be used.sigval_tis a union of:int sival_int; void __user *sival_ptr; 0 1 2 3 4 5 6 7 +---+---+---+---+---+---+---+---+ | sival_ptr | (64bit) +---+---+---+---+---+---+---+---+ | sival_ptr | (32bit) +---+---+---+---+ | sival_int | +---+---+---+---+The signal code solves the compat issues for
sigval_tby storing the 32bit pointers insival_int. So they meaningful bits are guaranteed to be in the low 32bits, just like the 32bitsival_ptr.After a bunch of build
BUG_ONsto verify my reasonable assumptions of but the siginfo layout are actually true, the code that generates the siginfo just copies asigval_ttosi_pid. And assumes the code in the usb stack placed the pointer in the proper part of thesigval_twhen it read the information from userspace.
Quoted 4 lines
Quoted 2 lines
I don't know if that helps make it easy to understand but I figured I would give it a shot.
I think I understand now. Thanks.
Welcome. I am trying to make this stuff comprehensible.
Eric
Quoted 148 lines · Eric W. Biederman
The usb support for asyncio encoded one of it's values in the wrong field. It should have used
si_valuebut instead usedsi_addrwhich is not present in the_rtunion member of struct siginfo.The practical result of this is that on a 64bit big endian kernel when delivering a signal to a 32bit process the
si_addrfield is set to NULL, instead of the expected pointer value.This issue can not be fixed in
copy_siginfo_to_user32as the usb usage of the the_sigfault(akasi_addr) member of the siginfo union whenSI_ASYNCIOis set is incompatible with the POSIX and glibc usage of the_rtmember of the siginfo union.Therefore replace
kill_pid_info_as_credwithkill_pid_usb_asyncioa dedicated function for this one specific case. There are no other users ofkill_pid_info_as_credso this specialization should have no impact on the amount of code in the kernel. Havekill_pid_usb_asynciotake instead of asiginfo_twhich is difficult and error prone, 3 arguments, a signal number, an errno value, and an address enconded as asigval_t. The encoding of the address as asigval_tallows the code that reads the userspace request for a signal to handle this compat issue along with all of the other compat issues.Add
BUILD_BUG_ONsinkernel/signal.cto ensure that we can now place the pointer value at the insi_pid(instead ofsi_addr). That is the code now verifies thatsi_pidandsi_addralways occur at the same location. Further the code veries that for native structures a value placed insi_pidand spilling intosi_uidwill appear in userspace insi_addr(on a byte by byte copy of siginfo or a field by field copy of siginfo). The code also verifies that for a 64bit kernel and a 32bit userspace the 32bit pointer will fit insi_pid.I have used the
usbsig.cprogram below written by Alan Stern and slightly tweaked by me to run on a big endian machine to verify the issue exists (on sparc64) and to confirm the patch below fixes the issue./*
usbsig.c-- test USB async signal delivery */static struct
usbdevfs_urburb;
static structusbdevfs_disconnectsignalds;
static volatilesig_atomic_tdone = 0;void
urb_handler(int sig, siginfo_t *info , void *ucontext){ printf("Got signal %d, signo %d errno %d code %d addr: %p urb: %p\n", sig, info->si_signo, info->si_errno, info->si_code, info->si_addr, &urb); printf("%s\n", (info->si_addr == &urb) ? "Good" : "Bad"); }void
ds_handler(int sig, siginfo_t *info , void *ucontext){ printf("Got signal %d, signo %d errno %d code %d addr: %p ds: %p\n", sig, info->si_signo, info->si_errno, info->si_code, info->si_addr, &ds); printf("%s\n", (info->si_addr == &ds) ? "Good" : "Bad"); done = 1; }int
main(int argc, char **argv){ char *devfilename; int fd; int rc; struct sigaction act; struct usb_ctrlrequest *req; void *ptr; char buf[80]; if (argc != 2) { fprintf(stderr, "Usage: usbsig device-file-name\n"); return 1; } devfilename = argv[1]; fd = open(devfilename, O_RDWR); if (fd == -1) { perror("Error opening device file"); return 1; } act.sa_sigaction = urb_handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_SIGINFO; rc = sigaction(SIGUSR1, &act, NULL); if (rc == -1) { perror("Error in sigaction"); return 1; } act.sa_sigaction = ds_handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_SIGINFO; rc = sigaction(SIGUSR2, &act, NULL); if (rc == -1) { perror("Error in sigaction"); return 1; } memset(&urb, 0, sizeof(urb)); urb.type = USBDEVFS_URB_TYPE_CONTROL; urb.endpoint = USB_DIR_IN | 0; urb.buffer = buf; urb.buffer_length = sizeof(buf); urb.signr = SIGUSR1; req = (struct usb_ctrlrequest *) buf; req->bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE; req->bRequest = USB_REQ_GET_DESCRIPTOR; req->wValue = htole16(USB_DT_DEVICE << 8); req->wIndex = htole16(0); req->wLength = htole16(sizeof(buf) - sizeof(*req)); rc = ioctl(fd, USBDEVFS_SUBMITURB, &urb); if (rc == -1) { perror("Error in SUBMITURB ioctl"); return 1; } rc = ioctl(fd, USBDEVFS_REAPURB, &ptr); if (rc == -1) { perror("Error in REAPURB ioctl"); return 1; } memset(&ds, 0, sizeof(ds)); ds.signr = SIGUSR2; ds.context = &ds; rc = ioctl(fd, USBDEVFS_DISCSIGNAL, &ds); if (rc == -1) { perror("Error in DISCSIGNAL ioctl"); return 1; } printf("Waiting for usb disconnect\n"); while (!done) { sleep(1); } close(fd); return 0; }
Quoted 8 lines
Cc Greg Kroah-Hartman <[email protected]>Cc Alan Stern <[email protected]>Cc Oliver Neukum <[email protected]>Fixes v2.3.39Signed-off-by "Eric W. Biederman" <[email protected]>---
Ugh, what a mess, thanks for doing this work: