Message ID | 20220503144425.2858110-1-keescook@chromium.org (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | Netdev Maintainers |
Headers | show |
Series | net: chelsio: cxgb4: Avoid potential negative array offset | expand |
On Tue, 3 May 2022 07:44:25 -0700 Kees Cook wrote: > Using min_t(int, ...) as a potential array index implies to the compiler > that negative offsets should be allowed. This is not the case, though. > Replace min_t() with clamp_t(). Fixes the following warning exposed > under future CONFIG_FORTIFY_SOURCE improvements: > Additionally remove needless cast from u8[] to char * in last strim() > call. > > Reported-by: kernel test robot <lkp@intel.com> > Link: https://lore.kernel.org/lkml/202205031926.FVP7epJM-lkp@intel.com > Fixes: fc9279298e3a ("cxgb4: Search VPD with pci_vpd_find_ro_info_keyword()") > Fixes: 24c521f81c30 ("cxgb4: Use pci_vpd_find_id_string() to find VPD ID string") Is it needed in the current release? > Cc: Raju Rangoju <rajur@chelsio.com> > Cc: "David S. Miller" <davem@davemloft.net> > Cc: Eric Dumazet <edumazet@google.com> > Cc: Jakub Kicinski <kuba@kernel.org> > Cc: Paolo Abeni <pabeni@redhat.com> > Cc: netdev@vger.kernel.org > Cc: stable@vger.kernel.org > Signed-off-by: Kees Cook <keescook@chromium.org> > --- > drivers/net/ethernet/chelsio/cxgb4/t4_hw.c | 10 +++++----- > 1 file changed, 5 insertions(+), 5 deletions(-) > > diff --git a/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c b/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c > index e7b4e3ed056c..f119ec7323e5 100644 > --- a/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c > +++ b/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c > @@ -2793,14 +2793,14 @@ int t4_get_raw_vpd_params(struct adapter *adapter, struct vpd_params *p) > goto out; > na = ret; > > - memcpy(p->id, vpd + id, min_t(int, id_len, ID_LEN)); > + memcpy(p->id, vpd + id, clamp_t(int, id_len, 0, ID_LEN)); The typing is needed because of the enum, right? The variable is unsigned, seems a little strange to use clamp(int, ..., 0, constant) min(unsigned int, ..., constant) will be equivalent with fewer branches. Is it just me? > strim(p->id); > - memcpy(p->sn, vpd + sn, min_t(int, sn_len, SERNUM_LEN)); > + memcpy(p->sn, vpd + sn, clamp_t(int, sn_len, 0, SERNUM_LEN)); > strim(p->sn); > - memcpy(p->pn, vpd + pn, min_t(int, pn_len, PN_LEN)); > + memcpy(p->pn, vpd + pn, clamp_t(int, pn_len, 0, PN_LEN)); > strim(p->pn); > - memcpy(p->na, vpd + na, min_t(int, na_len, MACADDR_LEN)); > - strim((char *)p->na); > + memcpy(p->na, vpd + na, clamp_t(int, na_len, 0, MACADDR_LEN)); > + strim(p->na); > > out: > vfree(vpd);
On Wed, May 04, 2022 at 08:13:58PM -0700, Jakub Kicinski wrote: > On Tue, 3 May 2022 07:44:25 -0700 Kees Cook wrote: > > Using min_t(int, ...) as a potential array index implies to the compiler > > that negative offsets should be allowed. This is not the case, though. > > Replace min_t() with clamp_t(). Fixes the following warning exposed > > under future CONFIG_FORTIFY_SOURCE improvements: > > > Additionally remove needless cast from u8[] to char * in last strim() > > call. > > > > Reported-by: kernel test robot <lkp@intel.com> > > Link: https://lore.kernel.org/lkml/202205031926.FVP7epJM-lkp@intel.com > > Fixes: fc9279298e3a ("cxgb4: Search VPD with pci_vpd_find_ro_info_keyword()") > > Fixes: 24c521f81c30 ("cxgb4: Use pci_vpd_find_id_string() to find VPD ID string") > > Is it needed in the current release? No, the build warning isn't in the current release, but I'm expecting to enable the next step of the FORTIFY work in the coming merge window. > > - memcpy(p->id, vpd + id, min_t(int, id_len, ID_LEN)); > > + memcpy(p->id, vpd + id, clamp_t(int, id_len, 0, ID_LEN)); > > The typing is needed because of the enum, right? The variable is > unsigned, seems a little strange to use clamp(int, ..., 0, constant) > min(unsigned int, ..., constant) will be equivalent with fewer branches. > Is it just me? Yes, due to the enum, but you're right; this could just use min_t(uint... I'll respin!
On Wed, May 04, 2022 at 08:13:58PM -0700, Jakub Kicinski wrote: > On Tue, 3 May 2022 07:44:25 -0700 Kees Cook wrote: > > diff --git a/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c b/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c > > index e7b4e3ed056c..f119ec7323e5 100644 > > --- a/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c > > +++ b/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c > > @@ -2793,14 +2793,14 @@ int t4_get_raw_vpd_params(struct adapter *adapter, struct vpd_params *p) > > goto out; > > na = ret; > > > > - memcpy(p->id, vpd + id, min_t(int, id_len, ID_LEN)); > > + memcpy(p->id, vpd + id, clamp_t(int, id_len, 0, ID_LEN)); > > The typing is needed because of the enum, right? The variable is > unsigned, seems a little strange to use clamp(int, ..., 0, constant) > min(unsigned int, ..., constant) will be equivalent with fewer branches. > Is it just me? I just chased down the origin of "unsigned int", but it's actually a u16 out of the VPD: static u16 pci_vpd_lrdt_size(const u8 *lrdt) { return get_unaligned_le16(lrdt + 1); } static int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt, unsigned int *size) { ... unsigned int lrdt_len = pci_vpd_lrdt_size(buf + i); ... *size = lrdt_len; I'm not sure why it was expanded to unsigned int size, maybe in other call sites it was easier to deal with for possible math, etc? Anyway, doesn't need changing. I'll send the int/unsigned int shortly...
diff --git a/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c b/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c index e7b4e3ed056c..f119ec7323e5 100644 --- a/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c +++ b/drivers/net/ethernet/chelsio/cxgb4/t4_hw.c @@ -2793,14 +2793,14 @@ int t4_get_raw_vpd_params(struct adapter *adapter, struct vpd_params *p) goto out; na = ret; - memcpy(p->id, vpd + id, min_t(int, id_len, ID_LEN)); + memcpy(p->id, vpd + id, clamp_t(int, id_len, 0, ID_LEN)); strim(p->id); - memcpy(p->sn, vpd + sn, min_t(int, sn_len, SERNUM_LEN)); + memcpy(p->sn, vpd + sn, clamp_t(int, sn_len, 0, SERNUM_LEN)); strim(p->sn); - memcpy(p->pn, vpd + pn, min_t(int, pn_len, PN_LEN)); + memcpy(p->pn, vpd + pn, clamp_t(int, pn_len, 0, PN_LEN)); strim(p->pn); - memcpy(p->na, vpd + na, min_t(int, na_len, MACADDR_LEN)); - strim((char *)p->na); + memcpy(p->na, vpd + na, clamp_t(int, na_len, 0, MACADDR_LEN)); + strim(p->na); out: vfree(vpd);
Using min_t(int, ...) as a potential array index implies to the compiler that negative offsets should be allowed. This is not the case, though. Replace min_t() with clamp_t(). Fixes the following warning exposed under future CONFIG_FORTIFY_SOURCE improvements: In file included from include/linux/string.h:253, from include/linux/bitmap.h:11, from include/linux/cpumask.h:12, from include/linux/smp.h:13, from include/linux/lockdep.h:14, from include/linux/rcupdate.h:29, from include/linux/rculist.h:11, from include/linux/pid.h:5, from include/linux/sched.h:14, from include/linux/delay.h:23, from drivers/net/ethernet/chelsio/cxgb4/t4_hw.c:35: drivers/net/ethernet/chelsio/cxgb4/t4_hw.c: In function 't4_get_raw_vpd_params': include/linux/fortify-string.h:46:33: warning: '__builtin_memcpy' pointer overflow between offset 29 and size [2147483648, 4294967295] [-Warray-bounds] 46 | #define __underlying_memcpy __builtin_memcpy | ^ include/linux/fortify-string.h:388:9: note: in expansion of macro '__underlying_memcpy' 388 | __underlying_##op(p, q, __fortify_size); \ | ^~~~~~~~~~~~~ include/linux/fortify-string.h:433:26: note: in expansion of macro '__fortify_memcpy_chk' 433 | #define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \ | ^~~~~~~~~~~~~~~~~~~~ drivers/net/ethernet/chelsio/cxgb4/t4_hw.c:2796:9: note: in expansion of macro 'memcpy' 2796 | memcpy(p->id, vpd + id, min_t(int, id_len, ID_LEN)); | ^~~~~~ include/linux/fortify-string.h:46:33: warning: '__builtin_memcpy' pointer overflow between offset 0 and size [2147483648, 4294967295] [-Warray-bounds] 46 | #define __underlying_memcpy __builtin_memcpy | ^ include/linux/fortify-string.h:388:9: note: in expansion of macro '__underlying_memcpy' 388 | __underlying_##op(p, q, __fortify_size); \ | ^~~~~~~~~~~~~ include/linux/fortify-string.h:433:26: note: in expansion of macro '__fortify_memcpy_chk' 433 | #define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \ | ^~~~~~~~~~~~~~~~~~~~ drivers/net/ethernet/chelsio/cxgb4/t4_hw.c:2798:9: note: in expansion of macro 'memcpy' 2798 | memcpy(p->sn, vpd + sn, min_t(int, sn_len, SERNUM_LEN)); | ^~~~~~ Additionally remove needless cast from u8[] to char * in last strim() call. Reported-by: kernel test robot <lkp@intel.com> Link: https://lore.kernel.org/lkml/202205031926.FVP7epJM-lkp@intel.com Fixes: fc9279298e3a ("cxgb4: Search VPD with pci_vpd_find_ro_info_keyword()") Fixes: 24c521f81c30 ("cxgb4: Use pci_vpd_find_id_string() to find VPD ID string") Cc: Raju Rangoju <rajur@chelsio.com> Cc: "David S. Miller" <davem@davemloft.net> Cc: Eric Dumazet <edumazet@google.com> Cc: Jakub Kicinski <kuba@kernel.org> Cc: Paolo Abeni <pabeni@redhat.com> Cc: netdev@vger.kernel.org Cc: stable@vger.kernel.org Signed-off-by: Kees Cook <keescook@chromium.org> --- drivers/net/ethernet/chelsio/cxgb4/t4_hw.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)