staging: comedi: INSN_CONFIG_DIGITAL_TRIG fixes
Subsystems (2)
cover staging: comedi: INSN_CONFIG_DIGITAL_TRIG fixes
These patches correct problems with INSN_CONFIG_DIGITAL_TRIG comedi configuration instructions in various comedi drivers, in particular the use of unconstrained bit shift amounts from userspace leading to undefined behaviour (although hopefully not the kernel crashy sort).
The patches have been marked for inclusion in the stable tree. Note that patch 4 changes a similar area of code to Dan Carpenter's commit ef75e14a6c93 ("staging: comedi: verify array index is correct before using it"), so I have indicated it as a prerequisite.
*Note to Greg KH*: I have based these patches on your "staging-linus" branch due to the prerequisite ef75e14a6c93 mentioned above being present in neither "staging-next" nor "staging-testing" at the time of posting.
1) staging: comedi: ni_6527: fix INSN_CONFIG_DIGITAL_TRIG support
2) staging: comedi: addi_apci_1032: check INSN_CONFIG_DIGITAL_TRIG shift
3) staging: comedi: addi_apci_1564: check INSN_CONFIG_DIGITAL_TRIG shift
4) staging: comedi: addi_apci_1500: check INSN_CONFIG_DIGITAL_TRIG shift
drivers/staging/comedi/drivers/addi_apci_1032.c | 20 ++++++++++++++------ drivers/staging/comedi/drivers/addi_apci_1500.c | 24 +++++++++++++++++++----- drivers/staging/comedi/drivers/addi_apci_1564.c | 20 ++++++++++++++------ drivers/staging/comedi/drivers/ni_6527.c | 2 +- 4 files changed, 48 insertions(+), 18 deletions(-)
PATCH 1/4 staging: comedi: ni_6527: fix INSN_CONFIG_DIGITAL_TRIG support
ni6527_intr_insn_config() processes INSN_CONFIG comedi instructions for the "interrupt" subdevice. When data[0] is INSN_CONFIG_DIGITAL_TRIG it is configuring the digital trigger. When data[2] is COMEDI_DIGITAL_TRIG_ENABLE_EDGES it is configuring rising and falling edge detection for the digital trigger, using a base channel number (or shift amount) in data[3], a rising edge bitmask in data[4] and falling edge bitmask in data[5].
If the base channel number (shift amount) is greater than or equal to the number of channels (24) of the digital input subdevice, there are no changes to the rising and falling edges, so the mask of channels to be changed can be set to 0, otherwise the mask of channels to be changed, and the rising and falling edge bitmasks are shifted by the base channel number before calling ni6527_set_edge_detection() to change the appropriate registers. Unfortunately, the code is comparing the base channel (shift amount) to the interrupt subdevice's number of channels
(1) instead of the digital input subdevice's number of channels (24). Fix it by comparing to 32 because all shift amounts for an unsigned int must be less than that and everything from bit 24 upwards is ignored by ni6527_set_edge_detection() anyway.
--- drivers/staging/comedi/drivers/ni_6527.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/comedi/drivers/ni_6527.c +1 -1
@@ -332,7 +332,7 @@ static int ni6527_intr_insn_config(struct comedi_device *dev,case COMEDI_DIGITAL_TRIG_ENABLE_EDGES :/* check shift amount */ shift = data[3 ]; -if (shift >= s->n_chan) { +if (shift >=32 ) { mask =0 ; rising =0 ; falling =0 ;
Signature · 1 line
2.27.0
PATCH 2/4 staging: comedi: addi_apci_1032: check INSN_CONFIG_DIGITAL_TRIG shift
In reply to Ian AbbottThe INSN_CONFIG comedi instruction with sub-instruction code INSN_CONFIG_DIGITAL_TRIG includes a base channel in data[3]. This is used as a right shift amount for other bitmask values without being checked. Shift amounts greater than or equal to 32 will result in undefined behavior. Add code to deal with this.
--- .../staging/comedi/drivers/addi_apci_1032.c | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
drivers/staging/comedi/drivers/addi_apci_1032.c +14 -6
@@ -106,14 +106,22 @@ static int apci1032_cos_insn_config(struct comedi_device *dev, unsigned int *data) {struct apci1032_private *devpriv = dev->private; -unsigned int shift, oldmask; +unsigned int shift, oldmask, himask, lomask;switch (data[0 ]) {case INSN_CONFIG_DIGITAL_TRIG :if (data[1 ] !=0 )return -EINVAL ; shift = data[3 ]; - oldmask = (1U << shift) -1 ; +if (shift <32 ) { + oldmask = (1U << shift) -1 ; + himask = data[4 ] << shift; + lomask = data[5 ] << shift; + }else { + oldmask =0xffffffffu ; + himask =0 ; + lomask =0 ; + }switch (data[2 ]) {case COMEDI_DIGITAL_TRIG_DISABLE : devpriv->ctrl =0 ;
@@ -136,8 +144,8 @@ static int apci1032_cos_insn_config(struct comedi_device *dev, devpriv->mode2 &= oldmask; }/* configure specified channels */ - devpriv->mode1 |= data[4 ] << shift; - devpriv->mode2 |= data[5 ] << shift; + devpriv->mode1 |= himask; + devpriv->mode2 |= lomask;break ;case COMEDI_DIGITAL_TRIG_ENABLE_LEVELS : if (devpriv->ctrl != (APCI1032_CTRL_INT_ENA |
@@ -154,8 +162,8 @@ static int apci1032_cos_insn_config(struct comedi_device *dev, devpriv->mode2 &= oldmask; }/* configure specified channels */ - devpriv->mode1 |= data[4 ] << shift; - devpriv->mode2 |= data[5 ] << shift; + devpriv->mode1 |= himask; + devpriv->mode2 |= lomask;break ;default :return -EINVAL ;
Signature · 1 line
2.27.0
PATCH 3/4 staging: comedi: addi_apci_1564: check INSN_CONFIG_DIGITAL_TRIG shift
In reply to Ian AbbottThe INSN_CONFIG comedi instruction with sub-instruction code INSN_CONFIG_DIGITAL_TRIG includes a base channel in data[3]. This is used as a right shift amount for other bitmask values without being checked. Shift amounts greater than or equal to 32 will result in undefined behavior. Add code to deal with this.
--- .../staging/comedi/drivers/addi_apci_1564.c | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
drivers/staging/comedi/drivers/addi_apci_1564.c +14 -6
@@ -331,14 +331,22 @@ static int apci1564_cos_insn_config(struct comedi_device *dev, unsigned int *data) {struct apci1564_private *devpriv = dev->private; -unsigned int shift, oldmask; +unsigned int shift, oldmask, himask, lomask;switch (data[0 ]) {case INSN_CONFIG_DIGITAL_TRIG :if (data[1 ] !=0 )return -EINVAL ; shift = data[3 ]; - oldmask = (1U << shift) -1 ; +if (shift <32 ) { + oldmask = (1U << shift) -1 ; + himask = data[4 ] << shift; + lomask = data[5 ] << shift; + }else { + oldmask =0xffffffffu ; + himask =0 ; + lomask =0 ; + }switch (data[2 ]) {case COMEDI_DIGITAL_TRIG_DISABLE : devpriv->ctrl =0 ;
@@ -362,8 +370,8 @@ static int apci1564_cos_insn_config(struct comedi_device *dev, devpriv->mode2 &= oldmask; }/* configure specified channels */ - devpriv->mode1 |= data[4 ] << shift; - devpriv->mode2 |= data[5 ] << shift; + devpriv->mode1 |= himask; + devpriv->mode2 |= lomask;break ;case COMEDI_DIGITAL_TRIG_ENABLE_LEVELS : if (devpriv->ctrl != (APCI1564_DI_IRQ_ENA |
@@ -380,8 +388,8 @@ static int apci1564_cos_insn_config(struct comedi_device *dev, devpriv->mode2 &= oldmask; }/* configure specified channels */ - devpriv->mode1 |= data[4 ] << shift; - devpriv->mode2 |= data[5 ] << shift; + devpriv->mode1 |= himask; + devpriv->mode2 |= lomask;break ;default :return -EINVAL ;
Signature · 1 line
2.27.0
PATCH 4/4 staging: comedi: addi_apci_1500: check INSN_CONFIG_DIGITAL_TRIG shift
In reply to Ian AbbottThe INSN_CONFIG comedi instruction with sub-instruction code INSN_CONFIG_DIGITAL_TRIG includes a base channel in data[3]. This is used as a right shift amount for other bitmask values without being checked. Shift amounts greater than or equal to 32 will result in undefined behavior. Add code to deal with this, adjusting the checks for invalid channels so that enabled channel bits that would have been lost by shifting are also checked for validity. Only channels 0 to 15 are valid.
--- .../staging/comedi/drivers/addi_apci_1500.c | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-)
drivers/staging/comedi/drivers/addi_apci_1500.c +19 -5
@@ -452,13 +452,14 @@ static int apci1500_di_cfg_trig(struct comedi_device *dev,struct apci1500_private *devpriv = dev->private;unsigned int trig = data[1 ];unsigned int shift = data[3 ]; -unsigned int hi_mask = data[4 ] << shift; -unsigned int lo_mask = data[5 ] << shift; -unsigned int chan_mask = hi_mask | lo_mask; -unsigned int old_mask = (1 << shift) -1 ; +unsigned int hi_mask; +unsigned int lo_mask; +unsigned int chan_mask; +unsigned int old_mask;unsigned int pm;unsigned int pt;unsigned int pp; +unsigned int invalid_chan;if (trig >1 ) { dev_dbg(dev->class_dev,
@@ -466,7 +467,20 @@ static int apci1500_di_cfg_trig(struct comedi_device *dev,return -EINVAL ; } -if (chan_mask >0xffff ) { +if (shift <=16 ) { + hi_mask = data[4 ] << shift; + lo_mask = data[5 ] << shift; + old_mask = (1U << shift) -1 ; + invalid_chan = (data[4 ] | data[5 ]) >> (16 - shift); + }else { + hi_mask =0 ; + lo_mask =0 ; + old_mask =0xffff ; + invalid_chan = data[4 ] | data[5 ]; + } + chan_mask = hi_mask | lo_mask; + +if (invalid_chan) {dev_dbg (dev->class_dev,"invalid digital trigger channel\n" );return -EINVAL ; }
Signature · 1 line
2.27.0
Greg Kroah-Hartman
maintainerQuoted 10 lines · Ian Abbott
These patches correct problems with
INSN_CONFIG_DIGITAL_TRIGcomedi configuration instructions in various comedi drivers, in particular the use of unconstrained bit shift amounts from userspace leading to undefined behaviour (although hopefully not the kernel crashy sort).The patches have been marked for inclusion in the stable tree. Note that patch 4 changes a similar area of code to Dan Carpenter's commit
ef75e14a6c93("staging: comedi: verify array index is correct before using it"), so I have indicated it as a prerequisite.
Quoted 4 lines
*Note to Greg KH*: I have based these patches on your "staging-linus" branch due to the prerequisite
ef75e14a6c93mentioned above being present in neither "staging-next" nor "staging-testing" at the time of posting.
Thanks for the note, all now queued up to the proper branch.
greg k-h