Wrap some tvb_get_ptr calls in crc32c_tvb_offset_calculate().

This eliminates some tvb_get_ptr calls from dissectors, as part of
trying to minimize if not eliminate its use in dissectors (it's a bit of
what's called an "attractive nuisance", in that users who don't
understand what it does - and doesn't! - do may use it in ways that are
unsafe, e.g. thinking you can use it to get a null-terminated string
from a packet).

It also eliminates the possibility of passing one length to
tvb_get_ptr() and another to crc32c_calculate().

Change-Id: I8a07168d0bc088b45d607e00c5bb1d98421ebc73
Reviewed-on: https://code.wireshark.org/review/3488
Reviewed-by: Guy Harris <guy@alum.mit.edu>
This commit is contained in:
Guy Harris 2014-08-07 20:38:11 -07:00
parent c10396dbbf
commit 20c0fffc92
5 changed files with 31 additions and 7 deletions

View File

@ -78,6 +78,17 @@ crc32_ccitt_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len,
return ( crc32_ccitt_seed(buf, len, seed) );
}
guint32
crc32c_tvb_offset_calculate(tvbuff_t *tvb, guint offset, guint len, guint32 seed)
{
const guint8* buf;
tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
buf = tvb_get_ptr(tvb, offset, len);
return ( crc32c_calculate(buf, len, seed) );
}
/*
* IEEE 802.x version (Ethernet and 802.11, at least) - byte-swap
* the result of "crc32()".

View File

@ -51,6 +51,17 @@ WS_DLL_PUBLIC guint32 crc32_ccitt_tvb_offset(tvbuff_t *tvb, guint offset, guint
@return The CRC32 CCITT checksum (using the given seed). */
WS_DLL_PUBLIC guint32 crc32_ccitt_tvb_seed(tvbuff_t *tvb, guint len, guint32 seed);
/** Compute CRC32C checksum of a tv buffer. If computing the
* checksum over multiple tv buffers and you want to feed the partial CRC32
* back in, remember to take the 1's complement of the partial CRC32 first.
@param tvb The tv buffer containing the data.
@param offset The offset into the tv buffer.
@param len The number of bytes to include in the computation.
@param seed The seed to use.
@return The CRC32C checksum (using the given seed). */
WS_DLL_PUBLIC guint32 crc32c_tvb_offset_calculate(tvbuff_t *tvb, guint offset,
guint len, guint32 seed);
/** Compute CRC32 CCITT checksum of a tv buffer. If computing the
* checksum over multiple tv buffers and you want to feed the partial CRC32
* back in, remember to take the 1's complement of the partial CRC32 first.

View File

@ -44,6 +44,7 @@
#include "packet-scsi.h"
#include <epan/wmem/wmem.h>
#include <epan/range.h>
#include <epan/crc32-tvb.h>
#include <wsutil/crc32.h>
void proto_register_iscsi(void);
@ -634,7 +635,7 @@ handleHeaderDigest(iscsi_session_t *iscsi_session, proto_item *ti, tvbuff_t *tvb
switch(iscsi_session->header_digest){
case ISCSI_DIGEST_CRC32:
if(available_bytes >= (headerLen + 4)) {
guint32 crc = ~crc32c_calculate(tvb_get_ptr(tvb, offset, headerLen), headerLen, CRC32C_PRELOAD);
guint32 crc = ~crc32c_tvb_offset_calculate(tvb, offset, headerLen, CRC32C_PRELOAD);
guint32 sent = tvb_get_ntohl(tvb, offset + headerLen);
if(crc == sent) {
proto_tree_add_uint_format_value(ti, hf_iscsi_HeaderDigest32, tvb, offset + headerLen, 4, sent, "0x%08x (Good CRC32)", sent);
@ -657,7 +658,7 @@ handleDataDigest(iscsi_session_t *iscsi_session, proto_item *ti, tvbuff_t *tvb,
switch (iscsi_session->data_digest){
case ISCSI_DIGEST_CRC32:
if(available_bytes >= (dataLen + 4)) {
guint32 crc = ~crc32c_calculate(tvb_get_ptr(tvb, offset, dataLen), dataLen, CRC32C_PRELOAD);
guint32 crc = ~crc32c_tvb_offset_calculate(tvb, offset, dataLen, CRC32C_PRELOAD);
guint32 sent = tvb_get_ntohl(tvb, offset + dataLen);
if(crc == sent) {
proto_tree_add_uint_format_value(ti, hf_iscsi_DataDigest32, tvb, offset + dataLen, 4, sent, "0x%08x (Good CRC32)", sent);
@ -2357,7 +2358,7 @@ dissect_iscsi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboolean chec
(iscsi_session->header_digest == ISCSI_DIGEST_AUTO)) {
guint32 crc;
/* we have enough data to test if HeaderDigest is enabled */
crc= ~crc32c_calculate(tvb_get_ptr(tvb, offset, 48+ahsLen*4), 48+ahsLen*4, CRC32C_PRELOAD);
crc= ~crc32c_tvb_offset_calculate(tvb, offset, 48+ahsLen*4, CRC32C_PRELOAD);
if(crc==tvb_get_ntohl(tvb,48+ahsLen*4)){
iscsi_session->header_digest = ISCSI_DIGEST_CRC32;
} else {
@ -2390,7 +2391,7 @@ dissect_iscsi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboolean chec
(iscsi_session->data_digest == ISCSI_DIGEST_AUTO)) {
guint32 crc;
/* we have enough data to test if DataDigest is enabled */
crc = ~crc32c_calculate(tvb_get_ptr(tvb, data_segment_offset, data_segment_len_padded), data_segment_len_padded, CRC32C_PRELOAD);
crc = ~crc32c_tvb_offset_calculate(tvb, data_segment_offset, data_segment_len_padded, CRC32C_PRELOAD);
if (crc == tvb_get_ntohl(tvb, data_segment_offset + data_segment_len_padded)) {
iscsi_session->data_digest = ISCSI_DIGEST_CRC32;
} else {

View File

@ -32,6 +32,7 @@
#include <epan/conversation.h>
#include <epan/dissectors/packet-tcp.h>
#include <epan/expert.h>
#include <epan/crc32-tvb.h>
#include <wsutil/crc32.h>
void proto_register_mpa(void);
@ -581,8 +582,7 @@ dissect_fpdu_crc(tvbuff_t *tvb, proto_tree *tree, mpa_state_t *state,
if (state->crc) {
crc = ~crc32c_calculate(tvb_get_ptr(tvb, 0, length), length,
CRC32C_PRELOAD);
crc = ~crc32c_tvb_offset_calculate(tvb, 0, length, CRC32C_PRELOAD);
sent_crc = tvb_get_ntohl(tvb, offset); /* crc start offset */

View File

@ -31,6 +31,7 @@
#include <epan/packet.h>
#include <epan/strutil.h>
#include <epan/wmem/wmem.h>
#include <epan/crc32-tvb.h>
#include <wsutil/crc32.h>
#include "packet-rtp.h"
#include "packet-rtcp.h"
@ -420,7 +421,7 @@ dissect_zrtp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
}
sent_crc = tvb_get_ntohl(tvb, msg_offset+checksum_offset);
calc_crc = ~crc32c_calculate(tvb_get_ptr(tvb, 0, msg_offset+checksum_offset), msg_offset+checksum_offset, CRC32C_PRELOAD);
calc_crc = ~crc32c_tvb_offset_calculate(tvb, 0, msg_offset+checksum_offset, CRC32C_PRELOAD);
if (sent_crc == calc_crc) {
ti = proto_tree_add_uint_format_value(zrtp_tree, hf_zrtp_checksum, tvb, msg_offset+checksum_offset, 4, sent_crc,