Skip to content

fix strlcpy len computation in netmap_bdg.c - #991

Merged
giuseppelettieri merged 1 commit into
luigirizzo:masterfrom
brian90013:fix-strlcpy-len
Oct 27, 2026
Merged

giuseppelettieri merged 1 commit into
luigirizzo:masterfrom
brian90013:fix-strlcpy-len

Conversation

@brian90013

Copy link
Copy Markdown
Contributor

In the recent commit cd23e3a, a call of strncpy(b->bdg_basename, name, namelen) was changed to strlcpy(b->bdg_basename, name, namelen). strncpy() will copy up to namelen bytes while strlcpy() will only copy to up namelen - 1 bytes. strlcpy() should typically be given the size of the destination buffer, not the number of characters in the source. But in this case, the goal is to truncate the name string before the colon ':' character.

I am proposing a two step solution. First strlcpy() is used with the length of the destination buffer to ensure there are no buffer overflows. Second, a '\0' is placed at the namelen position to terminate the name.

# Here is the current master showing the sixth character 'b' is truncated when only 5 characters are copied:
[ 8435.885982] 967.541670 [ 218] nm_find_bridge          copying name vale0b:1 of len 6
strlcpy() call
[ 8435.885998] 967.541689 [ 220] nm_find_bridge          bdg_basename is vale0

# Here is the proposed MR showing the entire name is copied
[15653.146446] 184.829196 [ 218] nm_find_bridge          copying name vale0a:1 of len 6
strlcpy() call
[15653.146461] 184.829214 [ 221] nm_find_bridge          bdg_basename is vale0a

An instance of strncpy() was converted to strlcpy() without modifying
the length parameter. strlcpy() will only copy up to len - 1 bytes so
the final byte was being dropped. Now use the size of the destination
buffer as the length then set '\0' at the appropriate offset.
@giuseppelettieri

Copy link
Copy Markdown
Collaborator

Thanks!

@giuseppelettieri
giuseppelettieri merged commit a352e31 into luigirizzo:master Oct 27, 2026
2 of 3 checks passed
@brian90013
brian90013 deleted the fix-strlcpy-len branch October 27, 2026 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants