Skip to content

Commit ede4ad6

Browse files
czp182alandekok
authored andcommitted
fix bounds checking, add corresponding unit tests
blksize was missing minimum check, if statement previously flags the 'end-of-file' packet as malformed, unit tests added to check empty data block signals end-of-file, as well as invalid block size where minimum is 8 (protocols/tftp: fix empty DATA packet rejection and missing blksize minimum check 470)
1 parent d7ae9e2 commit ede4ad6

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/protocols/tftp/decode.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ int fr_tftp_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, si
178178

179179
blksize = strtol((const char *)p, &p_end, 10);
180180

181-
if (p == (const uint8_t *)p_end || blksize > FR_TFTP_BLOCK_MAX_SIZE) {
181+
if ((p == (const uint8_t *)p_end) ||
182+
(blksize < FR_TFTP_BLOCK_MIN_SIZE) ||
183+
(blksize > FR_TFTP_BLOCK_MAX_SIZE)) {
182184
fr_strerror_printf("Invalid Block-Size %ld value", blksize);
183185
goto error;
184186
}
@@ -217,7 +219,7 @@ int fr_tftp_decode(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, si
217219
*/
218220
if (opcode != FR_OPCODE_VALUE_DATA) goto done;
219221

220-
if ((p + 2) >= end) goto error_malformed;
222+
if ((p + 2) > end) goto error_malformed;
221223

222224
p += 2;
223225

src/tests/unit/protocols/tftp/base.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ match Opcode = ::Data, Block = 4660, Data = 0x3132333435
4242
encode-proto -
4343
match 00 03 12 34 31 32 33 34 35
4444

45+
#
46+
# Server -> Client (Data) - empty data block signals end-of-file
47+
#
48+
decode-proto 00 03 00 01
49+
match Opcode = ::Data, Block = 1, Data = 0x
50+
4551
#
4652
# Client -> Server (Acknowledgement)
4753
#
@@ -61,4 +67,4 @@ encode-proto -
6167
match 00 05 00 04 4b 61 6c 6f 73 20 46 61 75 6c 74 00
6268

6369
count
64-
match 27
70+
match 29

src/tests/unit/protocols/tftp/error.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ match Packet contains malformed attribute
2121
decode-proto 00 01 00 6f 63 74 65 74 00 62 6c 6b 73 69 7a 65 7d 35 35 35 35 35 35 35
2222
match Packet contains malformed attribute
2323

24+
#
25+
# Client -> Server (Read-Request) - With invalid block-size. (min is 8)
26+
#
27+
decode-proto 00 01 66 00 6f 63 74 65 74 00 62 6c 6b 73 69 7a 65 00 37 00
28+
match Invalid Block-Size 7 value
29+
2430
#
2531
# Client -> Server (Read-Request) - With invalid block-size. (max is 65464)
2632
#
@@ -49,4 +55,4 @@ decode-proto 00 00 25 88
4955
match Invalid TFTP opcode 0000
5056

5157
count
52-
match 19
58+
match 21

0 commit comments

Comments
 (0)