public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Buggy __free(kfree) usage pattern already in tree
@ 2023-09-15  9:56 Alexey Dobriyan
  2023-09-15 10:09 ` Bartosz Golaszewski
  2023-09-15 17:04 ` Linus Torvalds
  0 siblings, 2 replies; 21+ messages in thread
From: Alexey Dobriyan @ 2023-09-15  9:56 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: linux-kernel, Bartosz Golaszewski, Linus Walleij, torvalds, akpm

__free() got some usage and some of the usage is buggy:

   832  static struct fwnode_handle *
   833  gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank,
   834                            struct fwnode_handle *parent)
   835  {
   838          char **line_names __free(kfree) = NULL;
		// returns NULL or ERR_PTR(-E)
   848          line_names = gpio_sim_make_line_names(bank, &line_names_size);
   849          if (IS_ERR(line_names))
   850                  return ERR_CAST(line_names);


This pattern will result in calling kfree() on error value.
And there are no compiler or sparse checking these things.

This test module demonstrates the landmine:

[  812.981089] ------------[ cut here ]------------
[  812.981597] WARNING: CPU: 0 PID: 1326 at mm/slab_common.c:991 free_large_kmalloc+0x50/0x80
[  813.013266] ---[ end trace 0000000000000000 ]---
[  813.013800] object pointer: 0xfffffffffffffff4

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/cleanup.h>

struct S {
	int x;
};

static struct S* f(void)
{
	struct S* s = kmalloc(sizeof(struct S), GFP_KERNEL);
	s = NULL;
	return s ?: ERR_PTR(-ENOMEM);
}

static int __init xxx_module_init(void)
{
	struct S *s __free(kfree) = NULL;
	s = f();
	if (IS_ERR(s)) {
		return PTR_ERR(s);
	}
	return 0;
}

static void __exit xxx_module_exit(void)
{
}
module_init(xxx_module_init);
module_exit(xxx_module_exit);
MODULE_LICENSE("GPL");

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2023-09-20 11:03 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-15  9:56 Buggy __free(kfree) usage pattern already in tree Alexey Dobriyan
2023-09-15 10:09 ` Bartosz Golaszewski
2023-09-15 17:04 ` Linus Torvalds
2023-09-15 17:22   ` Bartosz Golaszewski
2023-09-15 19:06     ` Linus Torvalds
2023-09-15 19:27       ` Bartosz Golaszewski
2023-09-15 20:03         ` Bartosz Golaszewski
2023-09-15 20:40           ` Linus Torvalds
2023-09-15 21:08             ` Peter Zijlstra
2023-09-15 21:18               ` Peter Zijlstra
2023-09-15 21:25                 ` Linus Torvalds
2023-09-15 21:22               ` Linus Torvalds
2023-09-15 21:32                 ` Peter Zijlstra
2023-09-15 21:50                   ` Linus Torvalds
2023-09-15 22:10                     ` Linus Torvalds
2023-09-15 22:13                     ` Peter Zijlstra
2023-09-19 12:57                       ` Peter Zijlstra
2023-09-19 12:59                         ` Peter Zijlstra
2023-09-19 13:10                           ` Peter Zijlstra
2023-09-19 19:35                             ` Peter Zijlstra
2023-09-20 11:02                               ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox