Skip to contentSkip to conversation

netfilter: nfnetlink: fix uninitialized local variable

PATCH In review icejl 3 participants 7 messages Aug 15, 2024

Subsystems (3)
lore
Share & tools
lore

icejl

author
#0 lore raw

PATCH 1/1 netfilter: nfnetlink: fix uninitialized local variable

In the nfnetlink_rcv_batch function, an uninitialized local variable extack is used, which results in using random stack data as a pointer. This pointer is then used to access the data it points to and return it as the request status, leading to an information leak. If the stack data happens to be an invalid pointer, it can cause a pointer access exception, triggering a kernel crash.

---
 net/netfilter/nfnetlink.c | 1 +
 1 file changed, 1 insertion(+)
net/netfilter/nfnetlink.c +1 -0
index 4abf660c7baf..b29b281f4b2c 100644
@@ -427,6 +427,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	nfnl_unlock(subsys_id);
 
+	memset(&extack, 0, sizeof(extack));
 	if (nlh->nlmsg_flags & NLM_F_ACK)
 		nfnl_err_add(&err_list, nlh, 0, &extack);
 
Signature · 1 line
2.34.1
#1 lore raw

There is a fix already traveling for this in a pull request.

Quoted 27 lines · icejl

In the nfnetlink_rcv_batch function, an uninitialized local variable extack is used, which results in using random stack data as a pointer. This pointer is then used to access the data it points to and return it as the request status, leading to an information leak. If the stack data happens to be an invalid pointer, it can cause a pointer access exception, triggering a kernel crash.

---
 net/netfilter/nfnetlink.c | 1 +
 1 file changed, 1 insertion(+)
net/netfilter/nfnetlink.c +1 -0
index 4abf660c7baf..b29b281f4b2c 100644
@@ -427,6 +427,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	nfnl_unlock(subsys_id);
 
+	memset(&extack, 0, sizeof(extack));
 	if (nlh->nlmsg_flags & NLM_F_ACK)
 		nfnl_err_add(&err_list, nlh, 0, &extack);
 
Signature · 1 line
2.34.1
#2 lore raw

For the record:

https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git/commit/?id=d1a7b382a9d3f0f3e5a80e0be2991c075fa4f618

Fixes bf2ac490d28c ("netfilter: nfnetlink: Handle ACK flags for batch messages")
Quoted 30 lines · Pablo Neira Ayuso

There is a fix already traveling for this in a pull request.

Quoted 27 lines · icejl

In the nfnetlink_rcv_batch function, an uninitialized local variable extack is used, which results in using random stack data as a pointer. This pointer is then used to access the data it points to and return it as the request status, leading to an information leak. If the stack data happens to be an invalid pointer, it can cause a pointer access exception, triggering a kernel crash.

---
 net/netfilter/nfnetlink.c | 1 +
 1 file changed, 1 insertion(+)
net/netfilter/nfnetlink.c +1 -0
index 4abf660c7baf..b29b281f4b2c 100644
@@ -427,6 +427,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	nfnl_unlock(subsys_id);
 
+	memset(&extack, 0, sizeof(extack));
 	if (nlh->nlmsg_flags & NLM_F_ACK)
 		nfnl_err_add(&err_list, nlh, 0, &extack);
 
Signature · 1 line
2.34.1
#3 lore raw
In reply to icejl
Quoted 17 lines · icejl

In the nfnetlink_rcv_batch function, an uninitialized local variable extack is used, which results in using random stack data as a pointer. This pointer is then used to access the data it points to and return it as the request status, leading to an information leak. If the stack data happens to be an invalid pointer, it can cause a pointer access exception, triggering a kernel crash.

---
 net/netfilter/nfnetlink.c | 1 +
 1 file changed, 1 insertion(+)
net/netfilter/nfnetlink.c +0 -0
index 4abf660c7baf..b29b281f4b2c 100644

net/netfilter/nfnetlink.c static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, · line 432

Quoted 6 lines
net/netfilter/nfnetlink.c +1 -0
@@ -427,6 +427,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	nfnl_unlock(subsys_id);
 
+	memset(&extack, 0, sizeof(extack));
 	if (nlh->nlmsg_flags & NLM_F_ACK)
 		nfnl_err_add(&err_list, nlh, 0, &extack);

There is a memset later in that function , inside the while (skb->len >= nlmsg_total_size(0)) loop. Should that one be removed?

#4 lore raw
Quoted 21 lines · Breno Leitao
Quoted 20 lines · icejl

In the nfnetlink_rcv_batch function, an uninitialized local variable extack is used, which results in using random stack data as a pointer. This pointer is then used to access the data it points to and return it as the request status, leading to an information leak. If the stack data happens to be an invalid pointer, it can cause a pointer access exception, triggering a kernel crash.

---
 net/netfilter/nfnetlink.c | 1 +
 1 file changed, 1 insertion(+)
net/netfilter/nfnetlink.c +0 -0
index 4abf660c7baf..b29b281f4b2c 100644
@@ -427,6 +427,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	nfnl_unlock(subsys_id);
 
Quoted 7 lines

net/netfilter/nfnetlink.c static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, · line 432

Quoted 3 lines
net/netfilter/nfnetlink.c +1 -0
+	memset(&extack, 0, sizeof(extack));
 	if (nlh->nlmsg_flags & NLM_F_ACK)
 		nfnl_err_add(&err_list, nlh, 0, &extack);

There is a memset later in that function , inside the while (skb->len >= nlmsg_total_size(0)) loop. Should that one be removed?

no, the batch contains a series of netlink message, each of them needs a fresh extack area which is zeroed.

this pointer leak only affects the recently released 6.10, older kernels are not affected.

#5 lore raw

Hello Pablo,

Quoted 30 lines · Pablo Neira Ayuso
Quoted 21 lines · Breno Leitao
Quoted 20 lines · icejl

In the nfnetlink_rcv_batch function, an uninitialized local variable extack is used, which results in using random stack data as a pointer. This pointer is then used to access the data it points to and return it as the request status, leading to an information leak. If the stack data happens to be an invalid pointer, it can cause a pointer access exception, triggering a kernel crash.

---
 net/netfilter/nfnetlink.c | 1 +
 1 file changed, 1 insertion(+)
net/netfilter/nfnetlink.c +0 -0
index 4abf660c7baf..b29b281f4b2c 100644
@@ -427,6 +427,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	nfnl_unlock(subsys_id);
 
Quoted 7 lines

net/netfilter/nfnetlink.c static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, · line 432

Quoted 3 lines
net/netfilter/nfnetlink.c +1 -0
+	memset(&extack, 0, sizeof(extack));
 	if (nlh->nlmsg_flags & NLM_F_ACK)
 		nfnl_err_add(&err_list, nlh, 0, &extack);

There is a memset later in that function , inside the while (skb->len >= nlmsg_total_size(0)) loop. Should that one be removed?

Quoted 2 lines

no, the batch contains a series of netlink message, each of them needs a fresh extack area which is zeroed.

Sorry, this is a bit unclear to me. This is the code I see in netnext/main:

	memset(&extack, 0, sizeof(extack));   // YOUR CHANGE

        if (nlh->nlmsg_flags & NLM_F_ACK)
                nfnl_err_add(&err_list, nlh, 0, &extack);

        while (skb->len >= nlmsg_total_size(0)) {
                int msglen, type;

                if (fatal_signal_pending(current)) {
                        nfnl_err_reset(&err_list);
                        err = -EINTR;
                        status = NFNL_BATCH_FAILURE;
                        goto done;
                }

-> memset(&extack, 0, sizeof(extack));

nfnl_err_add() does not change extack. Tht said, the second memset (last line in the snippet above), seems useless, doesn't it?

Thanks for the quick reply,
--breno

#6 lore raw
Quoted 55 lines · Breno Leitao

Hello Pablo,

Quoted 30 lines · Pablo Neira Ayuso
Quoted 21 lines · Breno Leitao
Quoted 20 lines · icejl

In the nfnetlink_rcv_batch function, an uninitialized local variable extack is used, which results in using random stack data as a pointer. This pointer is then used to access the data it points to and return it as the request status, leading to an information leak. If the stack data happens to be an invalid pointer, it can cause a pointer access exception, triggering a kernel crash.

---
 net/netfilter/nfnetlink.c | 1 +
 1 file changed, 1 insertion(+)
net/netfilter/nfnetlink.c +0 -0
index 4abf660c7baf..b29b281f4b2c 100644
@@ -427,6 +427,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	nfnl_unlock(subsys_id);
 
Quoted 7 lines

net/netfilter/nfnetlink.c static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, · line 432

Quoted 3 lines
net/netfilter/nfnetlink.c +1 -0
+	memset(&extack, 0, sizeof(extack));
 	if (nlh->nlmsg_flags & NLM_F_ACK)
 		nfnl_err_add(&err_list, nlh, 0, &extack);

There is a memset later in that function , inside the while (skb->len >= nlmsg_total_size(0)) loop. Should that one be removed?

Quoted 2 lines

no, the batch contains a series of netlink message, each of them needs a fresh extack area which is zeroed.

Sorry, this is a bit unclear to me. This is the code I see in netnext/main:

	memset(&extack, 0, sizeof(extack));   // YOUR CHANGE

        if (nlh->nlmsg_flags & NLM_F_ACK)
                nfnl_err_add(&err_list, nlh, 0, &extack);

        while (skb->len >= nlmsg_total_size(0)) {
                int msglen, type;

                if (fatal_signal_pending(current)) {
                        nfnl_err_reset(&err_list);
                        err = -EINTR;
                        status = NFNL_BATCH_FAILURE;
                        goto done;
                }
Quoted 5 lines

-> memset(&extack, 0, sizeof(extack));

nfnl_err_add() does not change extack. Tht said, the second memset (last line in the snippet above), seems useless, doesn't it?

Processing continues on error, several errors can be reported to userspace.

message A1 fails (set extack)
...
message An fails too (but does not set extack)

if extack is not reset, then message B gets a misleading error report that was set by message A.

Some error paths do not set extack, eg. EINVAL.