First Commit
[librecmc/package-feed.git] / net / haproxy / patches / 0026-BUG-MEDIUM-peers-fix-use-after-free-in-peer_session_.patch
1 From ab45181e36b6c4f7d31c5284035937c2d0be37eb Mon Sep 17 00:00:00 2001
2 From: Willy Tarreau <w@1wt.eu>
3 Date: Mon, 31 Oct 2016 17:46:57 +0100
4 Subject: [PATCH 26/26] BUG/MEDIUM: peers: fix use after free in
5  peer_session_create()
6
7 In case of resource allocation error, peer_session_create() frees
8 everything allocated and returns a pointer to the stream/session that
9 was put back into the free pool. This stream/session is then assigned
10 to ps->{stream,session} with no error control. This means that it is
11 perfectly possible to have a new stream or session being both used for
12 a regular communication and for a peer at the same time.
13
14 In fact it is the only way (for now) to explain a CLOSE_WAIT on peers
15 connections that was caught in this dump with the stream interface in
16 SI_ST_CON state while the error field proves the state ought to have
17 been SI_ST_DIS, very likely indicating two concurrent accesses on the
18 same area :
19
20   0x7dbd50: [31/Oct/2016:17:53:41.267510] id=0 proto=tcpv4
21     flags=0x23006, conn_retries=0, srv_conn=(nil), pend_pos=(nil)
22     frontend=myhost2 (id=4294967295 mode=tcp), listener=? (id=0)
23     backend=<NONE> (id=-1 mode=-) addr=127.0.0.1:41432
24     server=<NONE> (id=-1) addr=127.0.0.1:8521
25     task=0x7dbcd8 (state=0x08 nice=0 calls=2 exp=<NEVER> age=1m5s)
26     si[0]=0x7dbf48 (state=CLO flags=0x4040 endp0=APPCTX:0x7d99c8 exp=<NEVER>, et=0x000)
27     si[1]=0x7dbf68 (state=CON flags=0x50 endp1=CONN:0x7dc0b8 exp=<NEVER>, et=0x020)
28     app0=0x7d99c8 st0=11 st1=0 st2=0 applet=<PEER>
29     co1=0x7dc0b8 ctrl=tcpv4 xprt=RAW data=STRM target=PROXY:0x7fe62028a010
30         flags=0x0020b310 fd=7 fd.state=22 fd.cache=0 updt=0
31     req=0x7dbd60 (f=0x80a020 an=0x0 pipe=0 tofwd=0 total=0)
32         an_exp=<NEVER> rex=<NEVER> wex=<NEVER>
33         buf=0x78a3c0 data=0x78a3d4 o=0 p=0 req.next=0 i=0 size=0
34     res=0x7dbda0 (f=0x80402020 an=0x0 pipe=0 tofwd=0 total=0)
35         an_exp=<NEVER> rex=<NEVER> wex=<NEVER>
36         buf=0x78a3c0 data=0x78a3d4 o=0 p=0 rsp.next=0 i=0 size=0
37
38 Special thanks to Arnaud Gavara who provided lots of valuable input and
39 ran some validation testing on this patch.
40
41 This fix must be backported to 1.6 and 1.5. Note that in 1.5 the
42 session is not assigned from within the function so some extra checks
43 may be needed in the callers.
44 (cherry picked from commit b21d08e2492bfbf9d2341ce9f148cb9845927862)
45 ---
46  src/peers.c | 2 +-
47  1 file changed, 1 insertion(+), 1 deletion(-)
48
49 diff --git a/src/peers.c b/src/peers.c
50 index db1f608..c8be59a 100644
51 --- a/src/peers.c
52 +++ b/src/peers.c
53 @@ -1747,7 +1747,7 @@ static struct stream *peer_session_create(struct peers *peers, struct peer *peer
54   out_free_appctx:
55         appctx_free(appctx);
56   out_close:
57 -       return s;
58 +       return NULL;
59  }
60  
61  /*
62 -- 
63 2.7.3
64