-
Notifications
You must be signed in to change notification settings - Fork 23
Cp prep #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cp prep #388
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,9 @@ | |
| #include <string.h> | ||
| #include <unistd.h> | ||
|
|
||
| // IFALIASZ is defined in linux/if.h | ||
| #define IFALIASZ 256 | ||
|
|
||
| static char socket_buf[BUFSIZ]; | ||
| static struct mnl_socket *nl_sock; | ||
| static int nl_seq; | ||
|
|
@@ -292,6 +295,30 @@ int netlink_set_addr_gen_mode_none(const char *ifname) { | |
| return netlink_send_req(nlh); | ||
| } | ||
|
|
||
| int netlink_set_ifalias(const char *ifname, const char *ifalias) { | ||
| char buf[NLMSG_SPACE(sizeof(struct ifinfomsg) + NLA_SPACE(IFALIASZ))]; | ||
| struct ifinfomsg *ifm; | ||
| struct nlmsghdr *nlh; | ||
| int ifindex; | ||
|
|
||
| ifindex = if_nametoindex(ifname); | ||
| if (!ifindex) | ||
| return errno_set(ENODEV); | ||
|
|
||
| memset(buf, 0, sizeof(buf)); | ||
| nlh = mnl_nlmsg_put_header(buf); | ||
| nlh->nlmsg_type = RTM_NEWLINK; | ||
| nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; | ||
|
|
||
| ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm)); | ||
| ifm->ifi_family = AF_UNSPEC; | ||
| ifm->ifi_index = ifindex; | ||
|
|
||
| mnl_attr_put_strz(nlh, IFLA_IFALIAS, ifalias); | ||
|
|
||
| return netlink_send_req(nlh); | ||
| } | ||
|
Comment on lines
+298
to
+320
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate ifalias length before constructing the message. The function doesn't check if the Add validation before constructing the message: int netlink_set_ifalias(const char *ifname, const char *ifalias) {
char buf[NLMSG_SPACE(sizeof(struct ifinfomsg) + NLA_SPACE(IFALIASZ))];
struct ifinfomsg *ifm;
struct nlmsghdr *nlh;
int ifindex;
+ if (strlen(ifalias) >= IFALIASZ)
+ return errno_set(EINVAL);
+
ifindex = if_nametoindex(ifname);
if (!ifindex)
return errno_set(ENODEV);🤖 Prompt for AI Agents |
||
|
|
||
| static void netlink_init(struct event_base *) { | ||
| nl_sock = mnl_socket_open(NETLINK_ROUTE); | ||
| if (!nl_sock) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Buffer overflow risk in implementation (netlink.c)
The declaration follows existing patterns. However, the implementation in
netlink.c:297-319lacks validation thatifaliasdoesn't exceedIFALIASZ-1bytes. Sincemnl_attr_put_strz(line 319) will copy the full string into a fixed-size buffer, an oversized input can overflow the message buffer.Apply this fix in
netlink.cafter line 302:int netlink_set_ifalias(const char *ifname, const char *ifalias) { char buf[NLMSG_SPACE(sizeof(struct ifinfomsg) + NLA_SPACE(IFALIASZ))]; struct ifinfomsg *ifm; struct nlmsghdr *nlh; int ifindex; + if (strlen(ifalias) >= IFALIASZ) + return errno_set(EINVAL); + ifindex = if_nametoindex(ifname);