]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: stick_table: Prevent conn_cur from underflowing
authorTim Duesterhus <tim@bastelstu.be>
Thu, 3 Jan 2019 23:11:59 +0000 (00:11 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 15 Jan 2019 14:34:49 +0000 (15:34 +0100)
When using the peers feature a race condition could prevent
a connection from being properly counted. When this connection
exits it is being "uncounted" nonetheless, leading to a possible
underflow (-1) of the conn_curr stick table entry in the following
scenario :

  - Connect to peer A     (A=1, B=0)
  - Peer A sends 1 to B   (A=1, B=1)
  - Kill connection to A  (A=0, B=1)
  - Connect to peer B     (A=0, B=2)
  - Peer A sends 0 to B   (A=0, B=0)
  - Peer B sends 0/2 to A (A=?, B=0)
  - Kill connection to B  (A=?, B=-1)
  - Peer B sends -1 to A  (A=-1, B=-1)

This fix may be backported to all supported branches.

include/proto/session.h
include/proto/stream.h

index d54e9454b55ae7086a112e036022497c832c9f65..0b7d08d9950fa0b6f7c6cf1bdee667eca43c9f4b 100644 (file)
@@ -62,7 +62,8 @@ static inline void session_store_counters(struct session *sess)
                if (ptr) {
                        HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
 
-                       stktable_data_cast(ptr, conn_cur)--;
+                       if (stktable_data_cast(ptr, conn_cur) > 0)
+                               stktable_data_cast(ptr, conn_cur)--;
 
                        HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
 
index f44820977055642f897a0e7feaa5f4771bdd68b6..a8c29921425f994a545832f3f4ef967e4adf7730 100644 (file)
@@ -103,7 +103,8 @@ static inline void stream_store_counters(struct stream *s)
                if (ptr) {
                        HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
 
-                       stktable_data_cast(ptr, conn_cur)--;
+                       if (stktable_data_cast(ptr, conn_cur) > 0)
+                               stktable_data_cast(ptr, conn_cur)--;
 
                        HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
 
@@ -141,7 +142,8 @@ static inline void stream_stop_content_counters(struct stream *s)
                if (ptr) {
                        HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
 
-                       stktable_data_cast(ptr, conn_cur)--;
+                       if (stktable_data_cast(ptr, conn_cur) > 0)
+                               stktable_data_cast(ptr, conn_cur)--;
 
                        HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);