Skip to content

Commit

Permalink
driver core: Fix size calculation of symlink name for devlink_(add|re…
Browse files Browse the repository at this point in the history
…move)_symlinks()

devlink_(add|remove)_symlinks() kzalloc() memory to save symlink name
for both supplier and consumer, but do not explicitly take into account
consumer's prefix "consumer:", so cause disadvantages listed below:
1) it seems wrong for the algorithm to calculate memory size
2) readers maybe need to count characters one by one of both prefix
   strings to confirm calculated memory size
3) it is relatively easy to introduce new bug if either prefix string
   is modified in future
solved by taking into account consumer's prefix as well.

Signed-off-by: Zijun Hu <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
zijun-hu authored and gregkh committed Jul 31, 2024
1 parent 8400291 commit 4ea5e9d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions drivers/base/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,11 @@ static int devlink_add_symlinks(struct device *dev)
len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
strlen(dev_bus_name(con)) + strlen(dev_name(con)));
len += strlen(":");
len += strlen("supplier:") + 1;
/*
* we kzalloc() memory for symlink name of both supplier and
* consumer, so explicitly take into account both prefix.
*/
len += max(strlen("supplier:"), strlen("consumer:")) + 1;
buf = kzalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
Expand Down Expand Up @@ -623,7 +627,7 @@ static void devlink_remove_symlinks(struct device *dev)
len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
strlen(dev_bus_name(con)) + strlen(dev_name(con)));
len += strlen(":");
len += strlen("supplier:") + 1;
len += max(strlen("supplier:"), strlen("consumer:")) + 1;
buf = kzalloc(len, GFP_KERNEL);
if (!buf) {
WARN(1, "Unable to properly free device link symlinks!\n");
Expand Down

0 comments on commit 4ea5e9d

Please sign in to comment.