Skip to content
Open
39 changes: 23 additions & 16 deletions source/firewall/firewall_ipv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ int numifs = sizeof(ifnames) / sizeof(*ifnames);
#define V6_PORTSCANPROTECT "v6_PortScanProtect"
#define V6_IPFLOODDETECT "v6_IPFloodDetect"

#ifdef _ONESTACK_PRODUCT_REQ_
#define COSA_DML_DHCPV6_CLIENT_IFNAME "erouter0"
#define COSA_DML_DHCPV6C_PREF_SYSEVENT_NAME "tr_"COSA_DML_DHCPV6_CLIENT_IFNAME"_dhcpv6_client_v6pref"
#if defined (_ONESTACK_PRODUCT_REQ_)
static char ipv6_delegation_prefix[129] ={0};
#endif
Comment thread
rirfha948 marked this conversation as resolved.
Comment thread
snayak002c marked this conversation as resolved.
/*
****************************************************************
Expand Down Expand Up @@ -271,7 +270,15 @@ int prepare_ipv6_firewall(const char *fw_file)
goto clean_up_files;
}


#if defined (_ONESTACK_PRODUCT_REQ_)
char sysEventName[256] ={0};
if (isFeatureSupportedInCurrentMode(FEATURE_IPV6_DELEGATION))
{
snprintf(sysEventName, sizeof(sysEventName), "tr_%s_dhcpv6_client_v6pref", current_wan_ifname);
sysevent_get(sysevent_fd, sysevent_token, sysEventName, ipv6_delegation_prefix, sizeof(ipv6_delegation_prefix));
}
Comment thread
rirfha948 marked this conversation as resolved.
#endif

#ifdef RDKB_EXTENDER_ENABLED

if (isExtProfile() == 0)
Expand Down Expand Up @@ -1263,22 +1270,22 @@ void do_ipv6_filter_table(FILE *fp){
#ifdef _ONESTACK_PRODUCT_REQ_
if(isFeatureSupportedInCurrentMode(FEATURE_IPV6_DELEGATION))
{
sysevent_get(sysevent_fd, sysevent_token, COSA_DML_DHCPV6C_PREF_SYSEVENT_NAME, prefix, sizeof(prefix));
snprintf(prefix, sizeof(prefix), "%s", ipv6_delegation_prefix);
}
Comment on lines 1270 to 1274
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefix is only initialized with prefix[0] = 0; and then filled via strncpy(..., sizeof(prefix) - 1). If ipv6_delegation_prefix is length >= sizeof(prefix)-1, strncpy won’t NUL-terminate and prefix[sizeof(prefix)-1] is uninitialized, leading to undefined behavior in later strlen/fprintf uses. Zero-initialize prefix (e.g., memset(prefix, 0, sizeof(prefix))) or explicitly set prefix[sizeof(prefix)-1] = '\0' after the copy (or use a guaranteed NUL-terminating copy helper).

Copilot uses AI. Check for mistakes.
else
{
sysevent_get(sysevent_fd, sysevent_token, "ipv6_prefix", prefix, sizeof(prefix));
sysevent_get(sysevent_fd, sysevent_token, "ipv6_prefix", prefix, sizeof(prefix));
}
#else
sysevent_get(sysevent_fd, sysevent_token, "ipv6_prefix", prefix, sizeof(prefix));
sysevent_get(sysevent_fd, sysevent_token, "ipv6_prefix", prefix, sizeof(prefix));
#endif
}

#else
#ifdef _ONESTACK_PRODUCT_REQ_
if(isFeatureSupportedInCurrentMode(FEATURE_IPV6_DELEGATION))
{
sysevent_get(sysevent_fd, sysevent_token, COSA_DML_DHCPV6C_PREF_SYSEVENT_NAME, prefix, sizeof(prefix));
snprintf(prefix, sizeof(prefix), "%s", ipv6_delegation_prefix);
}
Comment on lines 1285 to 1289
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same strncpy NUL-termination issue here: prefix isn’t fully initialized before the copy and strncpy(..., sizeof(prefix) - 1) can leave the destination unterminated when the source is long/truncated. Ensure prefix is zeroed or force prefix[sizeof(prefix)-1] = '\0' after copying (or use a NUL-terminating helper).

Copilot uses AI. Check for mistakes.
else
{
Expand All @@ -1300,7 +1307,7 @@ void do_ipv6_filter_table(FILE *fp){
#if defined (_COSA_FOR_BCI_) || defined (_ONESTACK_PRODUCT_REQ_)
/* adding forward rule for PD traffic */
#ifdef _ONESTACK_PRODUCT_REQ_
if(isFeatureSupportedInCurrentMode(FEATURE_IPV6_DELEGATION))
if (isFeatureSupportedInCurrentMode(FEATURE_IPV6_DELEGATION))
{
fprintf(fp, "-A FORWARD -s %s -i %s -j ACCEPT\n", prefix, lan_ifname);
if (strncasecmp(firewall_levelv6, "Custom", strlen("Custom")) == 0)
Expand Down Expand Up @@ -2143,22 +2150,22 @@ void applyRoutingRules(FILE* fp,ipv6_type type)
}
else
{
#ifdef _ONESTACK_PRODUCT_REQ_
if(isFeatureSupportedInCurrentMode(FEATURE_IPV6_DELEGATION))
#ifdef _ONESTACK_PRODUCT_REQ_
if(isFeatureSupportedInCurrentMode(FEATURE_IPV6_DELEGATION))
{
sysevent_get(sysevent_fd, sysevent_token, COSA_DML_DHCPV6C_PREF_SYSEVENT_NAME, prefix, sizeof(prefix));
snprintf(prefix, sizeof(prefix), "%s", ipv6_delegation_prefix);
}
else
{
sysevent_get(sysevent_fd, sysevent_token, "ipv6_prefix", prefix, sizeof(prefix));
}
#else
#else
sysevent_get(sysevent_fd, sysevent_token, "ipv6_prefix", prefix, sizeof(prefix));
#endif
#endif
}
if (strlen(prefix) != 0 )
if (strlen(prefix) != 0)
{
char *token_pref =NULL;
char *token_pref =NULL;
token_pref = strtok(prefix,"/");
for(i = 0; i < mesh_wan_ipv6_num; i++)
{
Expand Down
Loading