Project

General

Profile

Actions

Bug #42

closed

SMB app layer assumes int for pointer arith

Added by Brian Rectanus almost 15 years ago. Updated almost 15 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
-
Affected Versions:
Effort:
Difficulty:
Label:

Description

app-layer-smb.c assumes that pointer arith results in "int", but on 64-bit linux this is "long int".

EX: static int SMBGetByteCount(Flow *f, void *smb_state, AppLayerParserState *pstate, uint8_t *input, uint32_t input_len, AppLayerParserResult *output)

uint8_t *p = input;
...
SCReturnInt(p - input);

Should return "int", not "long int". On 64-bit Linux and causes compile to fail with debug on (due to debugging output around SCReturnInt() macro) when using '-Wall -Werror -Wextra -Wno-unused-parameter' on 64-bit linux:

if gcc -DHAVE_CONFIG_H -I. -I. -I.. -I/home/brectanus/projects/oisf/deps/include -Wall -Werror -Wextra -Wno-unused-parameter -g -Wall -fno-strict-aliasing -D_BSD_SOURCE -D__BSD_SOURCE -D__FAVOR_BSD -DHAVE_NET_ETHERNET_H -I /usr/include -DLIBPCAP_VERSION_MAJOR=1 -DUNITTESTS -DDEBUG -MT app-layer-smb.o -MD -MP -MF ".deps/app-layer-smb.Tpo" -c -o app-layer-smb.o app-layer-smb.c; \
then mv -f ".deps/app-layer-smb.Tpo" ".deps/app-layer-smb.Po"; else rm -f ".deps/app-layer-smb.Tpo"; exit 1; fi
cc1: warnings being treated as errors
app-layer-smb.c: In function ‘SMBGetByteCount’:
app-layer-smb.c:425: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
app-layer-smb.c:425: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
app-layer-smb.c: In function ‘SMBParseWordCount’:
app-layer-smb.c:458: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
app-layer-smb.c:458: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
app-layer-smb.c: In function ‘SMBParseByteCount’:
app-layer-smb.c:499: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
app-layer-smb.c:499: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
app-layer-smb.c: In function ‘NBSSParseHeader’:
app-layer-smb.c:543: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
app-layer-smb.c:543: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
app-layer-smb.c: In function ‘SMBParseHeader’:
app-layer-smb.c:701: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
app-layer-smb.c:701: error: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
make2: *** [app-layer-smb.o] Error 1


Files

0001-64-bit-portability.patch (22.2 KB) 0001-64-bit-portability.patch Kirby Kuehl, 01/04/2010 03:14 PM
0001-64-bit-fixes-second-try.patch (31.8 KB) 0001-64-bit-fixes-second-try.patch Kirby Kuehl, 01/04/2010 03:35 PM
0001-64-bit-portability.patch (29.7 KB) 0001-64-bit-portability.patch Kirby Kuehl, 01/04/2010 04:52 PM
0001-64-bit-portability.patch (33.5 KB) 0001-64-bit-portability.patch final patch, fixes infinite loop in unittest, uses uint32_t for SCReturnUInt() Kirby Kuehl, 01/05/2010 09:04 AM
0001-64-bit-portability.patch (33.5 KB) 0001-64-bit-portability.patch final patch, fixes infinite loop in unittest, uses uint32_t for SCReturnUInt() Kirby Kuehl, 01/05/2010 09:04 AM
0001-64-bit-portability.patch (33.5 KB) 0001-64-bit-portability.patch final patch, fixes infinite loop in unittest, uses uint32_t for SCReturnUInt() Kirby Kuehl, 01/05/2010 09:05 AM
Actions #1

Updated by Kirby Kuehl almost 15 years ago

In util-debug.h
Could you modify
#define SCReturnInt(x) do { \
if (sc_log_global_log_level >= SC_LOG_DEBUG) { \
SCLogDebug("Returning: %d ... <<", x); \
SCLogCheckFDFilterExit(FUNCTION); \
} \
return x; \
} while(0)

To look like this (NOTE the only difference is the inclusion of the 't'.

#define SCReturnInt(x) do { \
if (sc_log_global_log_level >= SC_LOG_DEBUG) { \
SCLogDebug("Returning: %td ... <<", x); \
SCLogCheckFDFilterExit(FUNCTION); \
} \
return x; \
} while(0)

In C99, such code can use %td; the t size modifier corresponds to the
ptrdiff_t type.

Actions #2

Updated by Kirby Kuehl almost 15 years ago

If that works, let me know and I will do a submit a correct patch. If it doesn't work, I will create a SCReturnLongInt() macro and cast the
difference to a long and use %ld

Actions #3

Updated by Brian Rectanus almost 15 years ago

It does solve the issue there, but there is the other issue of passing an int:

cc1: warnings being treated as errors
flow.c: In function ‘FlowClearMemory’:
flow.c:747: error: format ‘%td’ expects type ‘ptrdiff_t’, but argument 4 has type ‘int’
flow.c:747: error: format ‘%td’ expects type ‘ptrdiff_t’, but argument 4 has type ‘int’

While this may solve the warning for the debug output, I think the real issue is that the function returns "int" and you are passing a "long int" in the return macro. Probably better would be to do proper casting instead of assuming that it is an int? Though the issue is not there w/o debug because the compiler sees that it needs to be an int in the return. I prefer casts, which shows that the programmer knows the issue is there.

Actions #4

Updated by Kirby Kuehl almost 15 years ago

Please try the attached patch. It introduces a new SCReturnLongInt() macro. Rather than use ptrdiff_t or the t format specifier, I casted everything to long int.

I use pointer arithmetic throughout smb, smb2, and dcerpc decoding so I have also made the appropriate modifications there as well.

If this works well for you, we can close the ticket.

Actions #5

Updated by Kirby Kuehl almost 15 years ago

Grr. Wait. Another patch coming soon :)

Actions #6

Updated by Kirby Kuehl almost 15 years ago

Try this one. Note to Victor, I also compile cleanly on Fedora 12.

-Wall -Werror -Wextra -Wno-unused-parameter

Actions #7

Updated by Victor Julien almost 15 years ago

Wouldn't it make more sense to use a fixed size type like uint32_t?

Actions #8

Updated by Brian Rectanus almost 15 years ago

Kirby: That patch does indeed remove the warnings for me, however...

Victor: Agreed, uint32_t seems adequate (providing no negative returns). Though "int" seems fine as well, given proper handling/casts. Or maybe just use ptrdiff_t, but there is no return Macro for this.

Actions #9

Updated by Kirby Kuehl almost 15 years ago

Here is a modified version that uses uint32_t.

Actions #10

Updated by Brian Rectanus almost 15 years ago

That last patch works for me as well (I like it better).

Actions #11

Updated by Victor Julien almost 15 years ago

After applying the patch the unittest DCERPCParserTest01 gets stuck an an endless loop.

Actions #13

Updated by Victor Julien almost 15 years ago

  • Status changed from New to Closed

Patch applied, thanks guys!

Actions

Also available in: Atom PDF