Skip to content

Commit a499660

Browse files
carterkozakfl4via
authored andcommitted
UNDERTOW-2424: Prevent connection access-after-free allowed through ChunkedStreamSinkConduit
ChunkedStreamSinkConduit releases underlying connections for reuse via a conduit listener, however flush calls may still be passed through after writes are terminated and successfully flushed. This would cause requests to be clobbered when the connection had already been reused to send another request, breaking the threading model.
1 parent 8e75dd3 commit a499660

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

core/src/main/java/io/undertow/conduits/ChunkedStreamSinkConduit.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ int doWrite(final ByteBuffer src) throws IOException {
200200

201201
@Override
202202
public void truncateWrites() throws IOException {
203+
if (anyAreSet(state, FLAG_FINISHED)) {
204+
return;
205+
}
203206
try {
204207
if (lastChunkBuffer != null) {
205208
lastChunkBuffer.close();
@@ -259,6 +262,9 @@ public long transferFrom(final StreamSourceChannel source, final long count, fin
259262

260263
@Override
261264
public boolean flush() throws IOException {
265+
if (anyAreSet(state, FLAG_FINISHED)) {
266+
return true;
267+
}
262268
this.state |= FLAG_FIRST_DATA_WRITTEN;
263269
if (anyAreSet(state, FLAG_WRITES_SHUTDOWN)) {
264270
if (anyAreSet(state, FLAG_NEXT_SHUTDOWN)) {
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright 2024 Red Hat, Inc., and individual contributors
4+
* as indicated by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package io.undertow.conduits;
20+
21+
import io.undertow.connector.ByteBufferPool;
22+
import io.undertow.server.DefaultByteBufferPool;
23+
import io.undertow.testutils.category.UnitTest;
24+
import io.undertow.util.AbstractAttachable;
25+
import io.undertow.util.HeaderMap;
26+
import org.junit.Test;
27+
import org.junit.experimental.categories.Category;
28+
import org.xnio.XnioIoThread;
29+
import org.xnio.XnioWorker;
30+
import org.xnio.channels.StreamSourceChannel;
31+
import org.xnio.conduits.StreamSinkConduit;
32+
import org.xnio.conduits.WriteReadyHandler;
33+
34+
import java.io.IOException;
35+
import java.nio.ByteBuffer;
36+
import java.nio.channels.FileChannel;
37+
import java.nio.charset.StandardCharsets;
38+
import java.util.concurrent.TimeUnit;
39+
import java.util.concurrent.atomic.AtomicInteger;
40+
import java.util.concurrent.atomic.AtomicLong;
41+
42+
import static org.junit.Assert.*;
43+
44+
/**
45+
* Test case for UNDERTOW-2424.
46+
*/
47+
@Category(UnitTest.class)
48+
public class ChunkedStreamSinkConduitTest {
49+
50+
@Test
51+
public void testChunkedStreamSinkConduit() throws IOException {
52+
ByteBufferPool pool = new DefaultByteBufferPool(false, 1024, -1, -1);
53+
AtomicLong written = new AtomicLong();
54+
AtomicInteger flushes = new AtomicInteger();
55+
AtomicInteger listenerInvocations = new AtomicInteger();
56+
StreamSinkConduit next = new StreamSinkConduit() {
57+
58+
@Override
59+
public long transferFrom(FileChannel src, long position, long count) throws IOException {
60+
written.addAndGet(count);
61+
return count;
62+
}
63+
64+
@Override
65+
public long transferFrom(StreamSourceChannel source, long count, ByteBuffer throughBuffer) throws IOException {
66+
written.addAndGet(count);
67+
return count;
68+
}
69+
70+
@Override
71+
public int write(ByteBuffer src) {
72+
int remaining = src.remaining();
73+
src.position(src.position() + remaining);
74+
written.addAndGet(remaining);
75+
return remaining;
76+
}
77+
78+
@Override
79+
public long write(ByteBuffer[] srcs, int offs, int len) throws IOException {
80+
long total = 0;
81+
for (int i = offs; i < len; i++) {
82+
int written = write(srcs[i]);
83+
if (written == 0) {
84+
break;
85+
}
86+
total += written;
87+
}
88+
return total;
89+
}
90+
91+
@Override
92+
public int writeFinal(ByteBuffer src) throws IOException {
93+
return write(src);
94+
}
95+
96+
@Override
97+
public long writeFinal(ByteBuffer[] srcs, int offset, int length) throws IOException {
98+
return write(srcs, offset, length);
99+
}
100+
101+
@Override
102+
public void terminateWrites() {
103+
}
104+
105+
@Override
106+
public boolean isWriteShutdown() {
107+
return false;
108+
}
109+
110+
@Override
111+
public void resumeWrites() {
112+
}
113+
114+
@Override
115+
public void suspendWrites() {
116+
}
117+
118+
@Override
119+
public void wakeupWrites() {
120+
}
121+
122+
@Override
123+
public boolean isWriteResumed() {
124+
return false;
125+
}
126+
127+
@Override
128+
public void awaitWritable() {
129+
}
130+
131+
@Override
132+
public void awaitWritable(long time, TimeUnit timeUnit) {
133+
134+
}
135+
136+
@Override
137+
public XnioIoThread getWriteThread() {
138+
return null;
139+
}
140+
141+
@Override
142+
public void setWriteReadyHandler(WriteReadyHandler handler) {
143+
144+
}
145+
146+
@Override
147+
public void truncateWrites() {
148+
149+
}
150+
151+
@Override
152+
public boolean flush() {
153+
flushes.incrementAndGet();
154+
return true;
155+
}
156+
157+
@Override
158+
public XnioWorker getWorker() {
159+
return null;
160+
}
161+
};
162+
ConduitListener<ChunkedStreamSinkConduit> listener = channel -> listenerInvocations.incrementAndGet();
163+
ChunkedStreamSinkConduit conduit = new ChunkedStreamSinkConduit(next, pool, false, false, new HeaderMap(), listener, new AbstractAttachable() {});
164+
165+
assertEquals(5, conduit.write(ByteBuffer.wrap("Hello".getBytes(StandardCharsets.UTF_8))));
166+
assertEquals("Expected 11 bytes to be flushed including chunk headers", 11, written.get());
167+
assertEquals(0, flushes.get());
168+
conduit.terminateWrites();
169+
assertTrue(conduit.flush());
170+
int flushesAfterTerminate = flushes.get();
171+
assertTrue(conduit.flush());
172+
// UNDERTOW-2424: If this isn't the case, invocations from response wrappers may invoke flush on persistent
173+
// connections that are already being used to process other requests on other threads.
174+
assertEquals("Expected flushing after termination not to have any impact", flushesAfterTerminate, flushes.get());
175+
assertEquals(1, listenerInvocations.get());
176+
}
177+
}

0 commit comments

Comments
 (0)