linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Ivan Vasilyev <grabesstimme@gmail.com>,
	"Spencer E. Olson" <olsonse@umich.edu>,
	Ian Abbott <abbotti@mev.co.uk>
Subject: [PATCH 4.9 33/56] staging: comedi: ni_mio_common: Fix divide-by-zero for DIO cmdtest
Date: Mon,  1 Apr 2019 19:02:49 +0200	[thread overview]
Message-ID: <20190401170105.740851969@linuxfoundation.org> (raw)
In-Reply-To: <20190401170103.398401360@linuxfoundation.org>

4.9-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Ian Abbott <abbotti@mev.co.uk>

commit bafd9c64056cd034a1174dcadb65cd3b294ff8f6 upstream.

`ni_cdio_cmdtest()` validates Comedi asynchronous commands for the DIO
subdevice (subdevice 2) of supported National Instruments M-series
cards.  It is called when handling the `COMEDI_CMD` and `COMEDI_CMDTEST`
ioctls for this subdevice.  There are two causes for a possible
divide-by-zero error when validating that the `stop_arg` member of the
passed-in command is not too large.

The first cause for the divide-by-zero is that calls to
`comedi_bytes_per_scan()` are only valid once the command has been
copied to `s->async->cmd`, but that copy is only done for the
`COMEDI_CMD` ioctl.  For the `COMEDI_CMDTEST` ioctl, it will use
whatever was left there by the previous `COMEDI_CMD` ioctl, if any.
(This is very likely, as it is usual for the application to use
`COMEDI_CMDTEST` before `COMEDI_CMD`.) If there has been no previous,
valid `COMEDI_CMD` for this subdevice, then `comedi_bytes_per_scan()`
will return 0, so the subsequent division in `ni_cdio_cmdtest()` of
`s->async->prealloc_bufsz / comedi_bytes_per_scan(s)` will be a
divide-by-zero error.  To fix this error, call a new function
`comedi_bytes_per_scan_cmd(s, cmd)`, based on the existing
`comedi_bytes_per_scan(s)` but using a specified `struct comedi_cmd` for
its calculations.  (Also refactor `comedi_bytes_per_scan()` to call the
new function.)

Once the first cause for the divide-by-zero has been fixed, the second
cause is that `comedi_bytes_per_scan_cmd()` can legitimately return 0 if
the `scan_end_arg` member of the `struct comedi_cmd` being tested is 0.
Fix it by only performing the division (and validating that `stop_arg`
is no more than the maximum value) if `comedi_bytes_per_scan_cmd()`
returns a non-zero value.

The problem was reported on the COMEDI mailing list here:
https://groups.google.com/forum/#!topic/comedi_list/4t9WlHzMhKM

Reported-by: Ivan Vasilyev <grabesstimme@gmail.com>
Tested-by: Ivan Vasilyev <grabesstimme@gmail.com>
Fixes: f164cbf98fa8 ("staging: comedi: ni_mio_common: add finite regeneration to dio output")
Cc: <stable@vger.kernel.org> # 4.6+
Cc: Spencer E. Olson <olsonse@umich.edu>
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 drivers/staging/comedi/comedidev.h             |    2 +
 drivers/staging/comedi/drivers.c               |   33 +++++++++++++++++++++----
 drivers/staging/comedi/drivers/ni_mio_common.c |   10 +++++--
 3 files changed, 38 insertions(+), 7 deletions(-)

--- a/drivers/staging/comedi/comedidev.h
+++ b/drivers/staging/comedi/comedidev.h
@@ -984,6 +984,8 @@ int comedi_dio_insn_config(struct comedi
 			   unsigned int mask);
 unsigned int comedi_dio_update_state(struct comedi_subdevice *,
 				     unsigned int *data);
+unsigned int comedi_bytes_per_scan_cmd(struct comedi_subdevice *s,
+				       struct comedi_cmd *cmd);
 unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s);
 unsigned int comedi_nscans_left(struct comedi_subdevice *s,
 				unsigned int nscans);
--- a/drivers/staging/comedi/drivers.c
+++ b/drivers/staging/comedi/drivers.c
@@ -390,11 +390,13 @@ unsigned int comedi_dio_update_state(str
 EXPORT_SYMBOL_GPL(comedi_dio_update_state);
 
 /**
- * comedi_bytes_per_scan() - Get length of asynchronous command "scan" in bytes
+ * comedi_bytes_per_scan_cmd() - Get length of asynchronous command "scan" in
+ * bytes
  * @s: COMEDI subdevice.
+ * @cmd: COMEDI command.
  *
  * Determines the overall scan length according to the subdevice type and the
- * number of channels in the scan.
+ * number of channels in the scan for the specified command.
  *
  * For digital input, output or input/output subdevices, samples for
  * multiple channels are assumed to be packed into one or more unsigned
@@ -404,9 +406,9 @@ EXPORT_SYMBOL_GPL(comedi_dio_update_stat
  *
  * Returns the overall scan length in bytes.
  */
-unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s)
+unsigned int comedi_bytes_per_scan_cmd(struct comedi_subdevice *s,
+				       struct comedi_cmd *cmd)
 {
-	struct comedi_cmd *cmd = &s->async->cmd;
 	unsigned int num_samples;
 	unsigned int bits_per_sample;
 
@@ -423,6 +425,29 @@ unsigned int comedi_bytes_per_scan(struc
 	}
 	return comedi_samples_to_bytes(s, num_samples);
 }
+EXPORT_SYMBOL_GPL(comedi_bytes_per_scan_cmd);
+
+/**
+ * comedi_bytes_per_scan() - Get length of asynchronous command "scan" in bytes
+ * @s: COMEDI subdevice.
+ *
+ * Determines the overall scan length according to the subdevice type and the
+ * number of channels in the scan for the current command.
+ *
+ * For digital input, output or input/output subdevices, samples for
+ * multiple channels are assumed to be packed into one or more unsigned
+ * short or unsigned int values according to the subdevice's %SDF_LSAMPL
+ * flag.  For other types of subdevice, samples are assumed to occupy a
+ * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag.
+ *
+ * Returns the overall scan length in bytes.
+ */
+unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s)
+{
+	struct comedi_cmd *cmd = &s->async->cmd;
+
+	return comedi_bytes_per_scan_cmd(s, cmd);
+}
 EXPORT_SYMBOL_GPL(comedi_bytes_per_scan);
 
 static unsigned int __comedi_nscans_left(struct comedi_subdevice *s,
--- a/drivers/staging/comedi/drivers/ni_mio_common.c
+++ b/drivers/staging/comedi/drivers/ni_mio_common.c
@@ -3477,6 +3477,7 @@ static int ni_cdio_check_chanlist(struct
 static int ni_cdio_cmdtest(struct comedi_device *dev,
 			   struct comedi_subdevice *s, struct comedi_cmd *cmd)
 {
+	unsigned int bytes_per_scan;
 	int err = 0;
 	int tmp;
 
@@ -3506,9 +3507,12 @@ static int ni_cdio_cmdtest(struct comedi
 	err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
 	err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
 					   cmd->chanlist_len);
-	err |= comedi_check_trigger_arg_max(&cmd->stop_arg,
-					    s->async->prealloc_bufsz /
-					    comedi_bytes_per_scan(s));
+	bytes_per_scan = comedi_bytes_per_scan_cmd(s, cmd);
+	if (bytes_per_scan) {
+		err |= comedi_check_trigger_arg_max(&cmd->stop_arg,
+						    s->async->prealloc_bufsz /
+						    bytes_per_scan);
+	}
 
 	if (err)
 		return 3;



  parent reply	other threads:[~2019-04-01 17:28 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-01 17:02 [PATCH 4.9 00/56] 4.9.167-stable review Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 01/56] Bluetooth: Check L2CAP option sizes returned from l2cap_get_conf_opt Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 02/56] Bluetooth: Verify that l2cap_get_conf_opt provides large enough buffer Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 03/56] video: fbdev: Set pixclock = 0 in goldfishfb Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 04/56] cfg80211: size various nl80211 messages correctly Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 05/56] stmmac: copy unicast mac address to MAC registers Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 06/56] dccp: do not use ipv6 header for ipv4 flow Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 07/56] mISDN: hfcpci: Test both vendor & device ID for Digium HFC4S Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 08/56] net/packet: Set __GFP_NOWARN upon allocation in alloc_pg_vec Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 09/56] net: rose: fix a possible stack overflow Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 10/56] packets: Always register packet sk in the same order Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 11/56] tcp: do not use ipv6 header for ipv4 flow Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 12/56] vxlan: Dont call gro_cells_destroy() before device is unregistered Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 13/56] sctp: get sctphdr by offset in sctp_compute_cksum Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 14/56] mac8390: Fix mmio access size probe Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 15/56] tun: properly test for IFF_UP Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 16/56] tun: add a missing rcu_read_unlock() in error path Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 17/56] btrfs: remove WARN_ON in log_dir_items Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 18/56] btrfs: raid56: properly unmap parity page in finish_parity_scrub() Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 19/56] ARM: imx6q: cpuidle: fix bug that CPU might not wake up at expected time Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 20/56] powerpc: bpf: Fix generation of load/store DW instructions Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 21/56] NFSv4.1 dont free interrupted slot on open Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 22/56] net: dsa: qca8k: remove leftover phy accessors Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 23/56] ALSA: rawmidi: Fix potential Spectre v1 vulnerability Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 24/56] ALSA: seq: oss: Fix " Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 25/56] ALSA: pcm: Fix possible OOB access in PCM oss plugins Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 26/56] ALSA: pcm: Dont suspend stream in unrecoverable PCM state Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 27/56] fs/open.c: allow opening only regular files during execve() Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 28/56] scsi: sd: Fix a race between closing an sd device and sd I/O Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 29/56] scsi: sd: Quiesce warning if device does not report optimal I/O size Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 30/56] scsi: zfcp: fix rport unblock if deleted SCSI devices on Scsi_Host Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 31/56] scsi: zfcp: fix scsi_eh host reset with port_forced ERP for non-NPIV FCP devices Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 32/56] tty: atmel_serial: fix a potential NULL pointer dereference Greg Kroah-Hartman
2019-04-01 17:02 ` Greg Kroah-Hartman [this message]
2019-04-01 17:02 ` [PATCH 4.9 34/56] staging: vt6655: Remove vif check from vnt_interrupt Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 35/56] staging: vt6655: Fix interrupt race condition on device start up Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 36/56] serial: max310x: Fix to avoid potential NULL pointer dereference Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 37/56] serial: sh-sci: Fix setting SCSCR_TIE while transferring data Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 38/56] USB: serial: cp210x: add new device id Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 39/56] USB: serial: ftdi_sio: add additional NovaTech products Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 40/56] USB: serial: mos7720: fix mos_parport refcount imbalance on error path Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 41/56] USB: serial: option: set driver_info for SIM5218 and compatibles Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 42/56] USB: serial: option: add Olicard 600 Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.9 43/56] Disable kgdboc failed by echo space to /sys/module/kgdboc/parameters/kgdboc Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 44/56] fs/proc/proc_sysctl.c: fix NULL pointer dereference in put_links Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 45/56] gpio: adnp: Fix testing wrong value in adnp_gpio_direction_input Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 46/56] usb: common: Consider only available nodes for dr_mode Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 47/56] usb: host: xhci-rcar: Add XHCI_TRUST_TX_LENGTH quirk Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 48/56] perf intel-pt: Fix TSC slip Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 49/56] cpu/hotplug: Prevent crash when CPU bringup fails on CONFIG_HOTPLUG_CPU=n Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 50/56] x86/smp: Enforce CONFIG_HOTPLUG_CPU when SMP=y Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 51/56] KVM: Reject device ioctls from processes other than the VMs creator Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 52/56] KVM: x86: Emulate MSR_IA32_ARCH_CAPABILITIES on AMD hosts Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 53/56] USB: gadget: f_hid: fix deadlock in f_hidg_write() Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 54/56] xhci: Fix port resume done detection for SS ports with LPM enabled Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 55/56] Revert "USB: core: only clean up what we allocated" Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.9 56/56] arm64: support keyctl() system call in 32-bit mode Greg Kroah-Hartman
2019-04-01 21:03 ` [PATCH 4.9 00/56] 4.9.167-stable review kernelci.org bot
2019-04-02  3:01 ` Naresh Kamboju
2019-04-02  9:03 ` Jon Hunter
2019-04-02 19:04 ` Guenter Roeck
2019-04-02 23:54 ` shuah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190401170105.740851969@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=abbotti@mev.co.uk \
    --cc=grabesstimme@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=olsonse@umich.edu \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).