diff options
author | jah <jah@FreeBSD.org> | 2016-05-29 07:14:51 +0000 |
---|---|---|
committer | jah <jah@FreeBSD.org> | 2016-05-29 07:14:51 +0000 |
commit | 653be251d7d13f335072502adbcc12caa43592ac (patch) | |
tree | 8d74e2b802c151e2d41f937bbc4cba2abe30bca8 /sys/dev/iicbus | |
parent | c23d573f29b40dc21606c413d0592c987b90f302 (diff) | |
download | FreeBSD-src-653be251d7d13f335072502adbcc12caa43592ac.zip FreeBSD-src-653be251d7d13f335072502adbcc12caa43592ac.tar.gz |
MFC r300258:
iic_rdwr_data->nmsgs is uint32_t, so limit the allowable number of messages to
prevent memory exhaustion and short allocations on 32-bit systems. Since
iicrdwr is intended to be a workalike of a Linux i2c-dev call, use the same
limit of 42 that Linux uses.
Also check the return value of copyin(9) to prevent unnecessary allocation in
the failure case.
Diffstat (limited to 'sys/dev/iicbus')
-rw-r--r-- | sys/dev/iicbus/iic.c | 9 | ||||
-rw-r--r-- | sys/dev/iicbus/iic.h | 2 |
2 files changed, 11 insertions, 0 deletions
diff --git a/sys/dev/iicbus/iic.c b/sys/dev/iicbus/iic.c index 84e1314..9ac7f74 100644 --- a/sys/dev/iicbus/iic.c +++ b/sys/dev/iicbus/iic.c @@ -299,9 +299,16 @@ iicrdwr(struct iic_cdevpriv *priv, struct iic_rdwr_data *d, int flags) parent = device_get_parent(iicdev); error = 0; + if (d->nmsgs > IIC_RDRW_MAX_MSGS) + return (EINVAL); + buf = malloc(sizeof(*d->msgs) * d->nmsgs, M_IIC, M_WAITOK); error = copyin(d->msgs, buf, sizeof(*d->msgs) * d->nmsgs); + if (error != 0) { + free(buf, M_IIC); + return (error); + } /* Alloc kernel buffers for userland data, copyin write data */ usrbufs = malloc(sizeof(void *) * d->nmsgs, M_IIC, M_WAITOK | M_ZERO); @@ -317,6 +324,8 @@ iicrdwr(struct iic_cdevpriv *priv, struct iic_rdwr_data *d, int flags) m->buf = NULL; if (error != 0) continue; + + /* m->len is uint16_t, so allocation size is capped at 64K. */ m->buf = malloc(m->len, M_IIC, M_WAITOK); if (!(m->flags & IIC_M_RD)) error = copyin(usrbufs[i], m->buf, m->len); diff --git a/sys/dev/iicbus/iic.h b/sys/dev/iicbus/iic.h index ba98d28..8ae1912 100644 --- a/sys/dev/iicbus/iic.h +++ b/sys/dev/iicbus/iic.h @@ -56,6 +56,8 @@ struct iic_rdwr_data { uint32_t nmsgs; }; +#define IIC_RDRW_MAX_MSGS 42 + #define I2CSTART _IOW('i', 1, struct iiccmd) /* start condition */ #define I2CSTOP _IO('i', 2) /* stop condition */ #define I2CRSTCARD _IOW('i', 3, struct iiccmd) /* reset the card */ |