Skip to contentSkip to conversation

lib/asn1_encoder.c:345 asn1_encode_integer_large_positive() error: we previously assumed 'integer' could be null (see line 331)

PATCH New Dan Carpenter 1 participant 1 message Jun 21, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 2/10 lib/asn1_encoder.c:345 asn1_encode_integer_large_positive() error: we previously assumed 'integer' could be null (see line 331)

tree:   https://github.com/l1k/linux doe
head:   a80fa5bd85f66ad910329bbe8bbb004f45b2bd68
commit: af175bd2d0ae71f1c4843947e7405ef8c5902ba1 [2/10] lib/asn1_encoder: Add a function to encode many byte integer values.
config: i386-randconfig-m021-20230620 (https://download.01.org/0day-ci/archive/20230621/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce: (https://download.01.org/0day-ci/archive/20230621/[email protected]/reproduce)

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 <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
lib/asn1_encoder.c:345 asn1_encode_integer_large_positive() error: we previously assumed 'integer' could be null (see line 331)

vim +/integer +345 lib/asn1_encoder.c

af175bd2d0ae71 Jonathan Cameron 2022-09-06  318  unsigned char *
af175bd2d0ae71 Jonathan Cameron 2022-09-06  319  asn1_encode_integer_large_positive(unsigned char *data,
af175bd2d0ae71 Jonathan Cameron 2022-09-06  320  				   const unsigned char *end_data, u32 tag,
af175bd2d0ae71 Jonathan Cameron 2022-09-06  321  				   const unsigned char *integer, int len)
af175bd2d0ae71 Jonathan Cameron 2022-09-06  322  {
af175bd2d0ae71 Jonathan Cameron 2022-09-06  323  	int data_len = end_data - data;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  324  	unsigned char *d = &data[2];
af175bd2d0ae71 Jonathan Cameron 2022-09-06  325  	bool found = false;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  326  	int i;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  327  
af175bd2d0ae71 Jonathan Cameron 2022-09-06  328  	if (WARN(tag > 30, "ASN.1 tag can't be > 30"))
af175bd2d0ae71 Jonathan Cameron 2022-09-06  329  		return ERR_PTR(-EINVAL);
af175bd2d0ae71 Jonathan Cameron 2022-09-06  330  

af175bd2d0ae71 Jonathan Cameron 2022-09-06 @331 if (!integer && WARN(len > 127, "BUG: recode tag is too big (>127)"))

Let's assume !integer and len == 127

af175bd2d0ae71 Jonathan Cameron 2022-09-06  332  		return ERR_PTR(-EINVAL);
af175bd2d0ae71 Jonathan Cameron 2022-09-06  333  
af175bd2d0ae71 Jonathan Cameron 2022-09-06  334  	if (IS_ERR(data))
af175bd2d0ae71 Jonathan Cameron 2022-09-06  335  		return data;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  336  
af175bd2d0ae71 Jonathan Cameron 2022-09-06  337  	if (data_len < 3)
af175bd2d0ae71 Jonathan Cameron 2022-09-06  338  		return ERR_PTR(-EINVAL);
af175bd2d0ae71 Jonathan Cameron 2022-09-06  339  
af175bd2d0ae71 Jonathan Cameron 2022-09-06  340  	data[0] = _tagn(UNIV, PRIM, tag);
af175bd2d0ae71 Jonathan Cameron 2022-09-06  341  	/* Leave space for length */
af175bd2d0ae71 Jonathan Cameron 2022-09-06  342  	data_len -= 2;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  343  
af175bd2d0ae71 Jonathan Cameron 2022-09-06  344  	for (i = 0; i < len; i++) {

af175bd2d0ae71 Jonathan Cameron 2022-09-06 @345 int byte = integer[i];

Then it would lead to a crash here

af175bd2d0ae71 Jonathan Cameron 2022-09-06  346  
af175bd2d0ae71 Jonathan Cameron 2022-09-06  347  		if (!found && byte == 0)
af175bd2d0ae71 Jonathan Cameron 2022-09-06  348  			continue;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  349  
af175bd2d0ae71 Jonathan Cameron 2022-09-06  350  		/*
af175bd2d0ae71 Jonathan Cameron 2022-09-06  351  		 * as per encode_integer
af175bd2d0ae71 Jonathan Cameron 2022-09-06  352  		 */
af175bd2d0ae71 Jonathan Cameron 2022-09-06  353  		if (!found && (byte & 0x80)) {
af175bd2d0ae71 Jonathan Cameron 2022-09-06  354  			*d++ = 0;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  355  			data_len--;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  356  		}
af175bd2d0ae71 Jonathan Cameron 2022-09-06  357  		found = true;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  358  		if (data_len == 0)
af175bd2d0ae71 Jonathan Cameron 2022-09-06  359  			return ERR_PTR(-EINVAL);
af175bd2d0ae71 Jonathan Cameron 2022-09-06  360  
af175bd2d0ae71 Jonathan Cameron 2022-09-06  361  		*d++ = byte;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  362  		data_len--;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  363  	}
af175bd2d0ae71 Jonathan Cameron 2022-09-06  364  
af175bd2d0ae71 Jonathan Cameron 2022-09-06  365  	data[1] = d - data - 2;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  366  
af175bd2d0ae71 Jonathan Cameron 2022-09-06  367  	return d;
af175bd2d0ae71 Jonathan Cameron 2022-09-06  368  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki