bincookie
Loading...
Searching...
No Matches
bincookie.h
Go to the documentation of this file.
1
7
8#ifndef _BINCOOKIE_H
9#define _BINCOOKIE_H
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
16// https://github.com/nodejs/http-parser/blob/master/http_parser.h#L32
17#include <sys/types.h>
18#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER < 1600) && \
19 !defined(__WINE__)
20#include <BaseTsd.h>
21#include <stddef.h>
22typedef unsigned __int32 uint32_t;
23typedef unsigned __int8 bool;
24#else
25#include <stdbool.h>
26#include <stdint.h>
27#endif
28#include <errno.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <time.h>
34
37
40#define bincookie_is_secure(cookie_ptr) (cookie_ptr->flags & secure)
42
45#define bincookie_domain_access_full(cookie_ptr) \
46 (((char *)cookie_ptr + cookie_ptr->domain_offset)[0] == '.')
47
48
51#define bincookie_iter_state_init(s) \
52 s.page_offset = 0; \
53 s.page_index = 0;
54
55
58#define bincookie_domain(c) ((char *)c + c->domain_offset)
60
63#define bincookie_path(c) \
64 ((char *)c + \
65 c->path_offset)
66
68
71#define bincookie_name(c) \
72 ((char *)c + \
73 c->name_offset)
74
76
79#define bincookie_value(c) ((char *)c + c->value_offset)
81#define APPLE_EPOCH_OFFSET 978307200
83
86#define bincookie_expiration_time(c) \
87 (c->expiry_date_epoch + APPLE_EPOCH_OFFSET)
88
89
92#define bincookie_creation_time(c) \
93 (c->create_date_epoch + APPLE_EPOCH_OFFSET)
94
96
98typedef enum {
99 secure = 1,
100 http_only = 1 << 2,
102
104typedef struct {
105 uint32_t page_offset;
106 uint32_t page_index;
108
109// Pages and cookies are found at file-controlled offsets with no alignment guarantee. Packing the
110// on-disk structures lowers their alignment to 1 without changing their layout.
111#pragma pack(push, 1)
113typedef struct {
114 uint32_t size;
115 unsigned char unk1[4];
117 unsigned char unk2[4];
118
119 uint32_t domain_offset;
120 uint32_t name_offset;
121 uint32_t path_offset;
122 uint32_t value_offset;
123
124 unsigned char unk[8];
125
129
131typedef struct {
132 unsigned char unk1[4];
134 uint32_t num_cookies;
135 uint32_t cookie_offsets[];
137
139typedef struct {
140 unsigned char magic[4];
141 uint32_t num_pages;
142 uint32_t page_sizes[];
144#pragma pack(pop)
145
148
159static inline bool bincookie_validate_pages(const bincookie_t *bc, size_t num_bytes) {
160 size_t page_offset = sizeof(bincookie_t) + (size_t)bc->num_pages * sizeof(uint32_t);
161 for (uint32_t i = 0; i < bc->num_pages; i++) {
162 if (page_offset > num_bytes || num_bytes - page_offset < sizeof(bincookie_page_t)) {
163 return false;
164 }
165 const bincookie_page_t *page = (const bincookie_page_t *)((const char *)bc + page_offset);
166 size_t offsets_room = num_bytes - page_offset - sizeof(bincookie_page_t);
167 if (offsets_room / sizeof(uint32_t) < page->num_cookies) {
168 return false;
169 }
170 for (uint32_t j = 0; j < page->num_cookies; j++) {
171 uint32_t cookie_offset = page->cookie_offsets[j];
172 if (cookie_offset > num_bytes - page_offset ||
173 num_bytes - page_offset - cookie_offset < sizeof(bincookie_cookie_t)) {
174 return false;
175 }
176 const bincookie_cookie_t *cookie =
177 (const bincookie_cookie_t *)((const char *)page + cookie_offset);
178 size_t cookie_pos = page_offset + cookie_offset;
179 uint32_t string_offsets[] = {cookie->domain_offset,
180 cookie->name_offset,
181 cookie->path_offset,
182 cookie->value_offset};
183 for (size_t k = 0; k < sizeof(string_offsets) / sizeof(string_offsets[0]); k++) {
184 if (string_offsets[k] >= num_bytes - cookie_pos) {
185 return false;
186 }
187 }
188 }
189 page_offset += bc->page_sizes[i];
190 }
191 return true;
192}
193
194
198static inline bincookie_t *const bincookie_init_file(FILE *fin) {
199 const long last_pos = ftell(fin);
200 rewind(fin);
201 char magic[4];
202 bincookie_t *cook = NULL;
203 size_t read = fread(magic, sizeof(magic), 1, fin);
204 bool is_cook = magic[0] == 'c' && magic[1] == 'o' && magic[2] == 'o' && magic[3] == 'k';
205 if (read != 1 || !is_cook) {
206 errno = EIO;
207 goto done;
208 }
209 // Read entire file
210 fseek(fin, 0, SEEK_END);
211 const long end_pos = ftell(fin);
212 // The fixed header, made up of the magic and num_pages, must be present before any
213 // file-derived field is dereferenced. The check also rejects the -1 returned by a failed
214 // ftell(). As a size_t, -1 wraps the allocation size below to zero.
215 if (end_pos < (long)sizeof(bincookie_t)) {
216 errno = EIO;
217 goto done;
218 }
219 size_t num_bytes = (size_t)end_pos;
220 // The extra zeroed byte terminates a cookie string that runs to the end of the file.
221 cook = (bincookie_t *)malloc(num_bytes + 1);
222 // LCOV_EXCL_START
223 if (cook == NULL) {
224 goto done;
225 } // LCOV_EXCL_STOP
226 memset(cook, 0, num_bytes + 1);
227 rewind(fin);
228 // LCOV_EXCL_START
229 if (fread(cook, num_bytes, 1, fin) != 1) {
230 free(cook);
231 cook = NULL;
232 goto done;
233 } // LCOV_EXCL_STOP
234 cook->num_pages = __builtin_bswap32(cook->num_pages);
235 // num_pages comes straight from the file; reject any value whose page size table cannot fit
236 // within the bytes actually read from disk before the table is dereferenced.
237 if (cook->num_pages > (num_bytes - sizeof(bincookie_t)) / sizeof(uint32_t)) {
238 free(cook);
239 cook = NULL;
240 errno = EIO;
241 goto done;
242 }
243 // Fix page size numbers
244 for (uint32_t i = 0; i < cook->num_pages; i++) {
245 *(cook->page_sizes + i) = __builtin_bswap32(*(cook->page_sizes + i));
246 }
247 // Every page, cookie offset table, and cookie offset is also file-controlled, so reject the
248 // file unless the whole structure stays within bounds.
249 if (!bincookie_validate_pages(cook, num_bytes)) {
250 free(cook);
251 cook = NULL;
252 errno = EIO;
253 goto done;
254 }
255done:
256 fseek(fin, last_pos, SEEK_SET);
257 return cook;
258}
259
260
264static inline bincookie_t *const bincookie_init_path(const char *file_path) {
265 FILE *binary_file = fopen(file_path, "rb");
266 if (!binary_file) {
267 return NULL;
268 }
269 bincookie_t *ret = bincookie_init_file(binary_file);
270 fclose(binary_file);
271 return ret;
272}
273
274
279static inline bincookie_page_t *const bincookie_iter_pages(const bincookie_t *bc,
280 bincookie_iter_state_t *const state) {
281 if (state->page_offset == 0) {
282 state->page_offset = (uint32_t)(4 + sizeof(uint32_t) + (sizeof(uint32_t) * bc->num_pages));
283 }
284 if (state->page_index < bc->num_pages) {
285 bincookie_page_t *page = (bincookie_page_t *)((char *)bc + state->page_offset);
286 state->page_offset += bc->page_sizes[state->page_index];
287 state->page_index++;
288 return page;
289 }
290 return NULL;
291}
292
293
299 unsigned int *i) {
300 if (*i < page->num_cookies) {
301 bincookie_cookie_t *ptr = (bincookie_cookie_t *)((char *)page + page->cookie_offsets[*i]);
302 (*i)++;
303 return ptr;
304 }
305 return NULL;
306}
307
308#ifdef __cplusplus
309}
310#endif
311
312#endif // _BINCOOKIE_H
static bincookie_page_t *const bincookie_iter_pages(const bincookie_t *bc, bincookie_iter_state_t *const state)
Iterate pages of a binarycookies file.
Definition bincookie.h:279
static bincookie_t *const bincookie_init_file(FILE *fin)
Read a binarycookies file.
Definition bincookie.h:198
static bincookie_t *const bincookie_init_path(const char *file_path)
Read a binarycookies file.
Definition bincookie.h:264
static bool bincookie_validate_pages(const bincookie_t *bc, size_t num_bytes)
Definition bincookie.h:159
static bincookie_cookie_t *const bincookie_iter_cookies(const bincookie_page_t *page, unsigned int *i)
Iterate cookies of a page.
Definition bincookie.h:298
bincookie_flag
Security enabled for a cookie.
Definition bincookie.h:98
@ http_only
Definition bincookie.h:100
@ secure
Definition bincookie.h:99
Keeps track of iteration state when iterating cookie file pages.
Definition bincookie.h:104
uint32_t page_offset
Definition bincookie.h:105
uint32_t page_index
Definition bincookie.h:106
Cookie page structure. A page can consist of 1 or more cookies.
Definition bincookie.h:131
unsigned char unk1[4]
Definition bincookie.h:132
uint32_t cookie_offsets[]
Definition bincookie.h:135
uint32_t num_cookies
Definition bincookie.h:134
bincookie file structure.
Definition bincookie.h:139
unsigned char magic[4]
Definition bincookie.h:140
uint32_t num_pages
Definition bincookie.h:141
uint32_t page_sizes[]
Definition bincookie.h:142