Commit 2b3a8b2
committed
Be more careful to not lose sync in the FE/BE protocol.
If any error occurred while we were in the middle of reading a protocol
message from the client, we could lose sync, and incorrectly try to
interpret a part of another message as a new protocol message. That will
usually lead to an "invalid frontend message" error that terminates the
connection. However, this is a security issue because an attacker might
be able to deliberately cause an error, inject a Query message in what's
supposed to be just user data, and have the server execute it.
We were quite careful to not have CHECK_FOR_INTERRUPTS() calls or other
operations that could ereport(ERROR) in the middle of processing a message,
but a query cancel interrupt or statement timeout could nevertheless cause
it to happen. Also, the V2 fastpath and COPY handling were not so careful.
It's very difficult to recover in the V2 COPY protocol, so we will just
terminate the connection on error. In practice, that's what happened
previously anyway, as we lost protocol sync.
To fix, add a new variable in pqcomm.c, PqCommReadingMsg, that is set
whenever we're in the middle of reading a message. When it's set, we cannot
safely ERROR out and continue running, because we might've read only part
of a message. PqCommReadingMsg acts somewhat similarly to critical sections
in that if an error occurs while it's set, the error handler will force the
connection to be terminated, as if the error was FATAL. It's not
implemented by promoting ERROR to FATAL in elog.c, like ERROR is promoted
to PANIC in critical sections, because we want to be able to use
PG_TRY/CATCH to recover and regain protocol sync. pq_getmessage() takes
advantage of that to prevent an OOM error from terminating the connection.
To prevent unnecessary connection terminations, add a holdoff mechanism
similar to HOLD/RESUME_INTERRUPTS() that can be used hold off query cancel
interrupts, but still allow die interrupts. The rules on which interrupts
are processed when are now a bit more complicated, so refactor
ProcessInterrupts() and the calls to it in signal handlers so that the
signal handlers always call it if ImmediateInterruptOK is set, and
ProcessInterrupts() can decide to not do anything if the other conditions
are not met.
Reported by Emil Lenngren. Patch reviewed by Noah Misch and Andres Freund.
Backpatch to all supported versions.
Security: CVE-2015-02441 parent 59b9198 commit 2b3a8b2Copy full SHA for 2b3a8b2
File tree
Expand file treeCollapse file tree
13 files changed
+244
-107
lines changedOpen diff view settings
Filter options
- src
- backend
- commands
- libpq
- postmaster
- replication
- storage/lmgr
- tcop
- utils
- error
- init
- include
- libpq
- tcop
Expand file treeCollapse file tree
13 files changed
+244
-107
lines changedOpen diff view settings
Collapse file
src/backend/commands/copy.c
Copy file name to clipboardExpand all lines: src/backend/commands/copy.c+14Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
410 | 410 | |
411 | 411 | |
412 | 412 | |
| 413 | + |
| 414 | + |
413 | 415 | |
414 | 416 | |
415 | 417 | |
| ||
420 | 422 | |
421 | 423 | |
422 | 424 | |
| 425 | + |
| 426 | + |
423 | 427 | |
424 | 428 | |
425 | 429 | |
| ||
606 | 610 | |
607 | 611 | |
608 | 612 | |
| 613 | + |
| 614 | + |
609 | 615 | |
610 | 616 | |
611 | 617 | |
| ||
615 | 621 | |
616 | 622 | |
617 | 623 | |
| 624 | + |
618 | 625 | |
619 | 626 | |
620 | 627 | |
| ||
2463 | 2470 | |
2464 | 2471 | |
2465 | 2472 | |
| 2473 | + |
| 2474 | + |
| 2475 | + |
| 2476 | + |
| 2477 | + |
| 2478 | + |
| 2479 | + |
2466 | 2480 | |
2467 | 2481 | |
2468 | 2482 | |
|
Collapse file
+3Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
625 | 625 | |
626 | 626 | |
627 | 627 | |
| 628 | + |
628 | 629 | |
629 | 630 | |
630 | 631 | |
| ||
849 | 850 | |
850 | 851 | |
851 | 852 | |
| 853 | + |
852 | 854 | |
853 | 855 | |
854 | 856 | |
| ||
1083 | 1085 | |
1084 | 1086 | |
1085 | 1087 | |
| 1088 | + |
1086 | 1089 | |
1087 | 1090 | |
1088 | 1091 | |
|
Collapse file
+74-2Lines changed: 74 additions & 2 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
127 | 127 | |
128 | 128 | |
129 | 129 | |
130 | | - |
131 | | - |
| 130 | + |
| 131 | + |
| 132 | + |
132 | 133 | |
133 | 134 | |
134 | 135 | |
| ||
177 | 178 | |
178 | 179 | |
179 | 180 | |
| 181 | + |
180 | 182 | |
181 | 183 | |
182 | 184 | |
| ||
916 | 918 | |
917 | 919 | |
918 | 920 | |
| 921 | + |
| 922 | + |
919 | 923 | |
920 | 924 | |
921 | 925 | |
| ||
954 | 958 | |
955 | 959 | |
956 | 960 | |
| 961 | + |
| 962 | + |
957 | 963 | |
958 | 964 | |
959 | 965 | |
| ||
1006 | 1012 | |
1007 | 1013 | |
1008 | 1014 | |
| 1015 | + |
| 1016 | + |
1009 | 1017 | |
1010 | 1018 | |
1011 | 1019 | |
| ||
1038 | 1046 | |
1039 | 1047 | |
1040 | 1048 | |
| 1049 | + |
| 1050 | + |
1041 | 1051 | |
1042 | 1052 | |
1043 | 1053 | |
| ||
1074 | 1084 | |
1075 | 1085 | |
1076 | 1086 | |
| 1087 | + |
| 1088 | + |
1077 | 1089 | |
1078 | 1090 | |
1079 | 1091 | |
| ||
1105 | 1117 | |
1106 | 1118 | |
1107 | 1119 | |
| 1120 | + |
| 1121 | + |
| 1122 | + |
| 1123 | + |
| 1124 | + |
| 1125 | + |
| 1126 | + |
| 1127 | + |
| 1128 | + |
| 1129 | + |
| 1130 | + |
| 1131 | + |
| 1132 | + |
| 1133 | + |
| 1134 | + |
| 1135 | + |
| 1136 | + |
| 1137 | + |
| 1138 | + |
| 1139 | + |
| 1140 | + |
| 1141 | + |
| 1142 | + |
| 1143 | + |
| 1144 | + |
| 1145 | + |
| 1146 | + |
| 1147 | + |
| 1148 | + |
| 1149 | + |
| 1150 | + |
| 1151 | + |
| 1152 | + |
| 1153 | + |
| 1154 | + |
| 1155 | + |
| 1156 | + |
| 1157 | + |
| 1158 | + |
| 1159 | + |
| 1160 | + |
| 1161 | + |
| 1162 | + |
| 1163 | + |
| 1164 | + |
| 1165 | + |
| 1166 | + |
| 1167 | + |
| 1168 | + |
| 1169 | + |
| 1170 | + |
| 1171 | + |
1108 | 1172 | |
1109 | 1173 | |
1110 | 1174 | |
| ||
1126 | 1190 | |
1127 | 1191 | |
1128 | 1192 | |
| 1193 | + |
| 1194 | + |
1129 | 1195 | |
1130 | 1196 | |
1131 | 1197 | |
| ||
1167 | 1233 | |
1168 | 1234 | |
1169 | 1235 | |
| 1236 | + |
| 1237 | + |
| 1238 | + |
1170 | 1239 | |
1171 | 1240 | |
1172 | 1241 | |
| ||
1184 | 1253 | |
1185 | 1254 | |
1186 | 1255 | |
| 1256 | + |
| 1257 | + |
| 1258 | + |
1187 | 1259 | |
1188 | 1260 | |
1189 | 1261 | |
|
Collapse file
src/backend/postmaster/postmaster.c
Copy file name to clipboardExpand all lines: src/backend/postmaster/postmaster.c+2Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
1761 | 1761 | |
1762 | 1762 | |
1763 | 1763 | |
| 1764 | + |
1764 | 1765 | |
1765 | 1766 | |
1766 | 1767 | |
| ||
1805 | 1806 | |
1806 | 1807 | |
1807 | 1808 | |
| 1809 | + |
1808 | 1810 | |
1809 | 1811 | |
1810 | 1812 | |
|
Collapse file
src/backend/replication/walsender.c
Copy file name to clipboardExpand all lines: src/backend/replication/walsender.c+12-23Lines changed: 12 additions & 23 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
1357 | 1357 | |
1358 | 1358 | |
1359 | 1359 | |
| 1360 | + |
1360 | 1361 | |
1361 | 1362 | |
1362 | 1363 | |
| ||
1369 | 1370 | |
1370 | 1371 | |
1371 | 1372 | |
| 1373 | + |
1372 | 1374 | |
1373 | 1375 | |
1374 | 1376 | |
| 1377 | + |
| 1378 | + |
| 1379 | + |
| 1380 | + |
| 1381 | + |
| 1382 | + |
| 1383 | + |
| 1384 | + |
| 1385 | + |
| 1386 | + |
1375 | 1387 | |
1376 | 1388 | |
1377 | 1389 | |
| ||
1407 | 1419 | |
1408 | 1420 | |
1409 | 1421 | |
1410 | | - |
1411 | | - |
1412 | | - |
1413 | | - |
1414 | | - |
1415 | | - |
1416 | | - |
1417 | | - |
1418 | | - |
1419 | | - |
1420 | 1422 | |
1421 | 1423 | |
1422 | 1424 | |
| ||
1453 | 1455 | |
1454 | 1456 | |
1455 | 1457 | |
1456 | | - |
1457 | | - |
1458 | | - |
1459 | | - |
1460 | | - |
1461 | | - |
1462 | | - |
1463 | | - |
1464 | | - |
1465 | | - |
1466 | | - |
1467 | | - |
1468 | | - |
1469 | 1458 | |
1470 | 1459 | |
1471 | 1460 | |
|
Collapse file
src/backend/storage/lmgr/proc.c
Copy file name to clipboardExpand all lines: src/backend/storage/lmgr/proc.c+7Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
655 | 655 | |
656 | 656 | |
657 | 657 | |
| 658 | + |
| 659 | + |
658 | 660 | |
659 | 661 | |
660 | 662 | |
661 | 663 | |
| 664 | + |
| 665 | + |
662 | 666 | |
| 667 | + |
663 | 668 | |
664 | 669 | |
665 | 670 | |
| ||
709 | 714 | |
710 | 715 | |
711 | 716 | |
| 717 | + |
| 718 | + |
712 | 719 | |
713 | 720 | |
714 | 721 | |
|
Collapse file
src/backend/tcop/fastpath.c
Copy file name to clipboardExpand all lines: src/backend/tcop/fastpath.c+1-28Lines changed: 1 addition & 28 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
75 | 75 | |
76 | 76 | |
77 | 77 | |
78 | | - |
| 78 | + |
79 | 79 | |
80 | 80 | |
81 | 81 | |
| ||
280 | 280 | |
281 | 281 | |
282 | 282 | |
283 | | - |
284 | | - |
285 | | - |
286 | | - |
287 | | - |
288 | | - |
289 | | - |
290 | | - |
291 | | - |
292 | | - |
293 | | - |
294 | | - |
295 | | - |
296 | | - |
297 | | - |
298 | | - |
299 | | - |
300 | | - |
301 | | - |
302 | | - |
303 | | - |
304 | | - |
305 | | - |
306 | | - |
307 | | - |
308 | | - |
309 | | - |
310 | 283 | |
311 | 284 | |
312 | 285 | |
|
0 commit comments