Skip to contentSkip to conversation

drivers/acpi/bus.c:339 acpi_osc_handshake() warn: variable dereferenced before check 'cap' (see line 332)

PATCH Dan Carpenter 2 participants 2 messages Dec 24, 2025 – Dec 26, 2025 ACPI

lore
Share & tools
lore

Dan Carpenter

author build bot
#0 lore raw

BCC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
TO: "Rafael J. Wysocki" <[email protected]>
CC: Jonathan Cameron <[email protected]>

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git bleeding-edge
head:   e08ef982088f28cd65654693f29ffe430a9d4d66
commit: e5322888e6bf4ec17964a93638c9b14433a2f6f1 [69/72] ACPI: bus: Rework the handling of \_SB._OSC platform features

:::::: branch date: 20 hours ago
:::::: commit date: 20 hours ago
config: x86_64-randconfig-161-20251224 (https://download.01.org/0day-ci/archive/20251224/[email protected]/config)
compiler: gcc-12 (Debian 12.4.0-5) 12.4.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 <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

New smatch warnings:
drivers/acpi/bus.c:339 acpi_osc_handshake() warn: variable dereferenced before check 'cap' (see line 332) drivers/acpi/bus.c:339 acpi_osc_handshake() warn: sizeof(NUMBER)?

Old smatch warnings:
drivers/acpi/bus.c:297 acpi_run_osc() warn: sizeof(NUMBER)?

vim +/cap +339 drivers/acpi/bus.c

70023de88c58a8 Shaohua Li        2009-10-29  327  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  328  static int acpi_osc_handshake(acpi_handle handle, const char *uuid_str,
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  329  			      int rev, struct acpi_buffer *cap)
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  330  {
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  331  	union acpi_object in_params[4], *out_obj;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22 @332  	size_t bufsize = cap->length / sizeof(u32);
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  333  	struct acpi_object_list input;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  334  	struct acpi_buffer output;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  335  	u32 *capbuf, *retbuf, test;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  336  	guid_t guid;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  337  	int ret, i;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  338  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22 @339  	if (!cap || cap->length < 2 * sizeof(32) || guid_parse(uuid_str, &guid))
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  340  		return -EINVAL;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  341  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  342  	/* First evaluate _OSC with OSC_QUERY_ENABLE set. */
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  343  	capbuf = cap->pointer;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  344  	capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  345  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  346  	ret = acpi_eval_osc(handle, &guid, rev, cap, in_params, &output);
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  347  	if (ret)
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  348  		return ret;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  349  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  350  	out_obj = output.pointer;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  351  	retbuf = (u32 *)out_obj->buffer.pointer;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  352  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  353  	if (acpi_osc_error_check(handle, &guid, rev, cap, retbuf)) {
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  354  		ret = -ENODATA;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  355  		goto out;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  356  	}
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  357  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  358  	/*
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  359  	 * Clear the feature bits in the capabilities buffer that have not been
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  360  	 * acknowledged and clear the return buffer.
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  361  	 */
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  362  	for (i = OSC_QUERY_DWORD + 1, test = 0; i < bufsize; i++) {
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  363  		capbuf[i] &= retbuf[i];
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  364  		test |= capbuf[i];
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  365  		retbuf[i] = 0;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  366  	}
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  367  	/*
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  368  	 * If none of the feature bits have been acknowledged, there's nothing
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  369  	 * more to do.  capbuf[] contains a feature mask of all zeros.
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  370  	 */
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  371  	if (!test)
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  372  		goto out;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  373  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  374  	retbuf[OSC_QUERY_DWORD] = 0;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  375  	/*
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  376  	 * Now evaluate _OSC again (directly) with OSC_QUERY_ENABLE clear and
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  377  	 * the updated input and output buffers used before.  Since the feature
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  378  	 * bits that were clear in the return buffer from the previous _OSC
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  379  	 * evaluation are also clear in the capabilities buffer now, this _OSC
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  380  	 * evaluation is not expected to fail.
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  381  	 */
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  382  	capbuf[OSC_QUERY_DWORD] = 0;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  383  	/* Reuse in_params[] populated by acpi_eval_osc(). */
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  384  	input.pointer = in_params;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  385  	input.count = 4;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  386  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  387  	if (ACPI_FAILURE(acpi_evaluate_object(handle, "_OSC", &input, &output))) {
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  388  		ret = -ENODATA;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  389  		goto out;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  390  	}
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  391  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  392  	/*
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  393  	 * Clear the feature bits in capbuf[] that have not been acknowledged.
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  394  	 * After that, capbuf[] contains the resultant feature mask.
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  395  	 */
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  396  	for (i = OSC_QUERY_DWORD + 1; i < bufsize; i++)
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  397  		capbuf[i] &= retbuf[i];
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  398  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  399  	if (retbuf[OSC_QUERY_DWORD] & OSC_ERROR_MASK) {
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  400  		/*
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  401  		 * Complain about the unexpected errors and print diagnostic
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  402  		 * information related to them.
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  403  		 */
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  404  		acpi_handle_err(handle, "_OSC: errors while processing control request\n");
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  405  		acpi_handle_err(handle, "_OSC: some features may be missing\n");
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  406  		acpi_osc_error_check(handle, &guid, rev, cap, retbuf);
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  407  	}
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  408  
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  409  out:
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  410  	ACPI_FREE(out_obj);
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  411  	return ret;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  412  }
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  413  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#1 lore raw
Quoted 43 lines · Dan Carpenter
tree:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git bleeding-edge
head:   e08ef982088f28cd65654693f29ffe430a9d4d66
commit: e5322888e6bf4ec17964a93638c9b14433a2f6f1 [69/72] ACPI: bus: Rework the handling of \_SB._OSC platform features
config: x86_64-randconfig-161-20251224 (https://download.01.org/0day-ci/archive/20251224/[email protected]/config)
compiler: gcc-12 (Debian 12.4.0-5) 12.4.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 <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

New smatch warnings:
drivers/acpi/bus.c:339 acpi_osc_handshake() warn: variable dereferenced before check 'cap' (see line 332) drivers/acpi/bus.c:339 acpi_osc_handshake() warn: sizeof(NUMBER)?

Old smatch warnings:
drivers/acpi/bus.c:297 acpi_run_osc() warn: sizeof(NUMBER)?

vim +/cap +339 drivers/acpi/bus.c

e5322888e6bf4e Rafael J. Wysocki 2025-12-22  328  static int acpi_osc_handshake(acpi_handle handle, const char *uuid_str,
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  329                              int rev, struct acpi_buffer *cap)
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  330  {
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  331        union acpi_object in_params[4], *out_obj;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22 @332        size_t bufsize = cap->length / sizeof(u32);
                                                                         ^^^^^^^^^^^

The cap pointer is dereferenced here.

e5322888e6bf4e Rafael J. Wysocki 2025-12-22  333        struct acpi_object_list input;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  334        struct acpi_buffer output;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  335        u32 *capbuf, *retbuf, test;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  336        guid_t guid;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  337        int ret, i;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  338
e5322888e6bf4e Rafael J. Wysocki 2025-12-22 @339        if (!cap || cap->length < 2 * sizeof(32) || guid_parse(uuid_str, &guid))
                                                            ^^^^
Checked too late.
                                                                                      ^^^^^^^^^^

Also this sizeof(32) should be sizeof(u32) although both are the same so the current code works.

Quoted 6 lines
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  340                return -EINVAL;
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  341
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  342        /* First evaluate _OSC with OSC_QUERY_ENABLE set. */
e5322888e6bf4e Rafael J. Wysocki 2025-12-22  343        capbuf = cap->pointer;

--

Thanks for catching this!

Interestingly enough, I have a tangentially related patch fixing both issues reported above. I'll send it shortly.