Skip to content

Commit a0b7859

Browse files
committed
pflog: use nd_ types in struct pfloghdr.
This 1) makes sure that GET_ macros are used to extract data from the structure (which they already were) and 2) avoids undefined behavior if the structure isn't aligned on the appropriate memory boundary. Fixes #1054. (The SNMP issues are fixed by changes for #1012.)
1 parent a028658 commit a0b7859

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

pflog.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,35 +115,35 @@ struct pf_addr {
115115
};
116116

117117
struct pfloghdr {
118-
uint8_t length;
119-
uint8_t af;
120-
uint8_t action;
121-
uint8_t reason;
118+
nd_uint8_t length;
119+
nd_uint8_t af;
120+
nd_uint8_t action;
121+
nd_uint8_t reason;
122122
char ifname[PFLOG_IFNAMSIZ];
123123
char ruleset[PFLOG_RULESET_NAME_SIZE];
124-
uint32_t rulenr;
125-
uint32_t subrulenr;
126-
uint32_t uid;
127-
int32_t pid;
128-
uint32_t rule_uid;
129-
int32_t rule_pid;
130-
uint8_t dir;
124+
nd_uint32_t rulenr;
125+
nd_uint32_t subrulenr;
126+
nd_uint32_t uid;
127+
nd_int32_t pid;
128+
nd_uint32_t rule_uid;
129+
nd_int32_t rule_pid;
130+
nd_uint8_t dir;
131131
#if defined(__OpenBSD__)
132-
uint8_t rewritten;
133-
uint8_t naf;
134-
uint8_t pad[1];
132+
nd_uint8_t rewritten;
133+
nd_uint8_t naf;
134+
nd_uint8_t pad[1];
135135
#else
136-
uint8_t pad[3];
136+
nd_uint8_t pad[3];
137137
#endif
138138
#if defined(__FreeBSD__)
139-
uint32_t ridentifier;
140-
uint8_t reserve;
141-
uint8_t pad2[3];
139+
nd_uint32_t ridentifier;
140+
nd_uint8_t reserve;
141+
nd_uint8_t pad2[3];
142142
#elif defined(__OpenBSD__)
143143
struct pf_addr saddr;
144144
struct pf_addr daddr;
145-
uint16_t sport;
146-
uint16_t dport;
145+
nd_uint16_t sport;
146+
nd_uint16_t dport;
147147
#endif
148148
};
149149

print-pflog.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
106106
uint32_t rulenr, subrulenr;
107107

108108
ndo->ndo_protocol = "pflog";
109-
rulenr = GET_BE_U_4(&hdr->rulenr);
110-
subrulenr = GET_BE_U_4(&hdr->subrulenr);
109+
rulenr = GET_BE_U_4(hdr->rulenr);
110+
subrulenr = GET_BE_U_4(hdr->subrulenr);
111111
if (subrulenr == (uint32_t)-1)
112112
ND_PRINT("rule %u/", rulenr);
113113
else {
@@ -117,9 +117,9 @@ pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
117117
}
118118

119119
ND_PRINT("%s: %s %s on ",
120-
tok2str(pf_reasons, "unkn(%u)", GET_U_1(&hdr->reason)),
121-
tok2str(pf_actions, "unkn(%u)", GET_U_1(&hdr->action)),
122-
tok2str(pf_directions, "unkn(%u)", GET_U_1(&hdr->dir)));
120+
tok2str(pf_reasons, "unkn(%u)", GET_U_1(hdr->reason)),
121+
tok2str(pf_actions, "unkn(%u)", GET_U_1(hdr->action)),
122+
tok2str(pf_directions, "unkn(%u)", GET_U_1(hdr->dir)));
123123
nd_printjnp(ndo, (const u_char*)hdr->ifname, PFLOG_IFNAMSIZ);
124124
ND_PRINT(": ");
125125
}
@@ -144,12 +144,13 @@ pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
144144

145145
#define MIN_PFLOG_HDRLEN 45
146146
hdr = (const struct pfloghdr *)p;
147-
if (GET_U_1(&hdr->length) < MIN_PFLOG_HDRLEN) {
147+
hdrlen = GET_U_1(hdr->length);
148+
if (hdrlen < MIN_PFLOG_HDRLEN) {
148149
ND_PRINT("[pflog: invalid header length!]");
149-
ndo->ndo_ll_hdr_len += GET_U_1(&hdr->length); /* XXX: not really */
150+
ndo->ndo_ll_hdr_len += hdrlen; /* XXX: not really */
150151
return;
151152
}
152-
hdrlen = roundup2(hdr->length, 4);
153+
hdrlen = roundup2(hdrlen, 4);
153154

154155
if (caplen < hdrlen) {
155156
nd_print_trunc(ndo);
@@ -163,7 +164,7 @@ pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
163164
pflog_print(ndo, hdr);
164165

165166
/* skip to the real packet */
166-
af = GET_U_1(&hdr->af);
167+
af = GET_U_1(hdr->af);
167168
length -= hdrlen;
168169
caplen -= hdrlen;
169170
p += hdrlen;

0 commit comments

Comments
 (0)