Re: [PATCH net-next 12/14] devlink: Throw extack messages on param value validation error

Dan Carpenter
2025-03-03T07:06:14Z

Hi Saeed,

kernel test robot noticed the following build warnings:

url: https://github.com/intel-lab-lkp/linux/commits/Saeed-Mahameed/devlink-define-enum-for-attr-types-of-dynamic-attributes/20250228-101818
base: net-next/main
patch link: https://lore.kernel.org/r/20250228021227.871993-13-saeed%40kernel.org
patch subject: [PATCH net-next 12/14] devlink: Throw extack messages on param value validation error
config: sparc-randconfig-r071-20250302 (https://download.01.org/0day-ci/archive/20250302/202503022006.eOu80RMF-lkp@intel.com/config)
compiler: sparc-linux-gcc (GCC) 14.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
| Closes: https://lore.kernel.org/r/202503022006.eOu80RMF-lkp@intel.com/

smatch warnings:
net/devlink/param.c:498 devlink_param_value_validate() error: we previously assumed 'param_data' could be null (see line 475)

vim +/param_data +498 net/devlink/param.c

ca557d9df319fed Saeed Mahameed 2025-02-27 461 static int
ca557d9df319fed Saeed Mahameed 2025-02-27 462 devlink_param_value_validate(struct genl_info *info,
ca557d9df319fed Saeed Mahameed 2025-02-27 463 enum devlink_param_type type)
ca557d9df319fed Saeed Mahameed 2025-02-27 464 {
ca557d9df319fed Saeed Mahameed 2025-02-27 465 struct netlink_ext_ack *extack = info->extack;
ca557d9df319fed Saeed Mahameed 2025-02-27 466 struct nlattr *param_data;
ca557d9df319fed Saeed Mahameed 2025-02-27 467 int len = 0;
ca557d9df319fed Saeed Mahameed 2025-02-27 468
ca557d9df319fed Saeed Mahameed 2025-02-27 469 if (type != DEVLINK_PARAM_TYPE_BOOL &&
ca557d9df319fed Saeed Mahameed 2025-02-27 470 GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_VALUE_DATA))
ca557d9df319fed Saeed Mahameed 2025-02-27 471 return -EINVAL;
ca557d9df319fed Saeed Mahameed 2025-02-27 472
ca557d9df319fed Saeed Mahameed 2025-02-27 473 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
ca557d9df319fed Saeed Mahameed 2025-02-27 474
ca557d9df319fed Saeed Mahameed 2025-02-27 @475 if (param_data)

This assumes param_data can be NULL

ca557d9df319fed Saeed Mahameed 2025-02-27 476 len = nla_len(param_data);
ca557d9df319fed Saeed Mahameed 2025-02-27 477
ca557d9df319fed Saeed Mahameed 2025-02-27 478 switch (type) {
ca557d9df319fed Saeed Mahameed 2025-02-27 479 case DEVLINK_PARAM_TYPE_U8:
ca557d9df319fed Saeed Mahameed 2025-02-27 480 if (len == sizeof(u8))
ca557d9df319fed Saeed Mahameed 2025-02-27 481 return 0;
ca557d9df319fed Saeed Mahameed 2025-02-27 482 NL_SET_ERR_MSG_FMT_MOD(extack,
ca557d9df319fed Saeed Mahameed 2025-02-27 483 "Expected uint8, got %d bytes", len);
ca557d9df319fed Saeed Mahameed 2025-02-27 484 break;
ca557d9df319fed Saeed Mahameed 2025-02-27 485 case DEVLINK_PARAM_TYPE_U16:
ca557d9df319fed Saeed Mahameed 2025-02-27 486 if (len == sizeof(u16))
ca557d9df319fed Saeed Mahameed 2025-02-27 487 return 0;
ca557d9df319fed Saeed Mahameed 2025-02-27 488 NL_SET_ERR_MSG_FMT_MOD(extack,
ca557d9df319fed Saeed Mahameed 2025-02-27 489 "Expected uint16, got %d bytes", len);
ca557d9df319fed Saeed Mahameed 2025-02-27 490 break;
ca557d9df319fed Saeed Mahameed 2025-02-27 491 case DEVLINK_PARAM_TYPE_U32:
ca557d9df319fed Saeed Mahameed 2025-02-27 492 if (len == sizeof(u32))
ca557d9df319fed Saeed Mahameed 2025-02-27 493 return 0;
ca557d9df319fed Saeed Mahameed 2025-02-27 494 NL_SET_ERR_MSG_FMT_MOD(extack,
ca557d9df319fed Saeed Mahameed 2025-02-27 495 "Expected uint32, got %d bytes", len);
ca557d9df319fed Saeed Mahameed 2025-02-27 496 break;
ca557d9df319fed Saeed Mahameed 2025-02-27 497 case DEVLINK_PARAM_TYPE_STRING:
ca557d9df319fed Saeed Mahameed 2025-02-27 @498 len = strnlen(nla_data(param_data), nla_len(param_data));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Unchecked dereferences

ca557d9df319fed Saeed Mahameed 2025-02-27 499
ca557d9df319fed Saeed Mahameed 2025-02-27 500 if (len < nla_len(param_data) &&
ca557d9df319fed Saeed Mahameed 2025-02-27 501 len < __DEVLINK_PARAM_MAX_STRING_VALUE)
ca557d9df319fed Saeed Mahameed 2025-02-27 502 return 0;
ca557d9df319fed Saeed Mahameed 2025-02-27 503 NL_SET_ERR_MSG_MOD(extack, "String too long");
ca557d9df319fed Saeed Mahameed 2025-02-27 504 break;
ca557d9df319fed Saeed Mahameed 2025-02-27 505 case DEVLINK_PARAM_TYPE_BOOL:
ca557d9df319fed Saeed Mahameed 2025-02-27 506 if (!len)
ca557d9df319fed Saeed Mahameed 2025-02-27 507 return 0;
ca557d9df319fed Saeed Mahameed 2025-02-27 508 NL_SET_ERR_MSG_MOD(extack, "Expected flag, got data");
ca557d9df319fed Saeed Mahameed 2025-02-27 509 break;
ca557d9df319fed Saeed Mahameed 2025-02-27 510 default:
ca557d9df319fed Saeed Mahameed 2025-02-27 511 NL_SET_ERR_MSG_FMT_MOD(extack,
ca557d9df319fed Saeed Mahameed 2025-02-27 512 "Not supported value type %d", type);
ca557d9df319fed Saeed Mahameed 2025-02-27 513 break;
ca557d9df319fed Saeed Mahameed 2025-02-27 514 }
ca557d9df319fed Saeed Mahameed 2025-02-27 515 return -EINVAL;
ca557d9df319fed Saeed Mahameed 2025-02-27 516 }

--
0-DAY CI Kernel Test Service