-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_kernel_printf.c
1530 lines (1371 loc) · 54.2 KB
/
check_kernel_printf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2015 Rasmus Villemoes.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#define _GNU_SOURCE
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include "smatch.h"
#include "smatch_slist.h"
#define spam(args...) do { \
if (option_spammy) \
sm_msg(args); \
} while (0)
static int my_id;
/*
* Much of this is taken directly from the kernel (mostly vsprintf.c),
* with a few modifications here and there.
*/
#define KERN_SOH_ASCII '\001'
typedef unsigned char u8;
typedef signed short s16;
#define SIGN 1 /* unsigned/signed, must be 1 */
#define LEFT 2 /* left justified */
#define PLUS 4 /* show plus */
#define SPACE 8 /* space if plus */
#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
enum format_type {
FORMAT_TYPE_NONE, /* Just a string part */
FORMAT_TYPE_WIDTH,
FORMAT_TYPE_PRECISION,
FORMAT_TYPE_CHAR,
FORMAT_TYPE_STR,
FORMAT_TYPE_PTR,
FORMAT_TYPE_PERCENT_CHAR,
FORMAT_TYPE_INVALID,
FORMAT_TYPE_LONG_LONG,
FORMAT_TYPE_ULONG,
FORMAT_TYPE_LONG,
FORMAT_TYPE_UBYTE,
FORMAT_TYPE_BYTE,
FORMAT_TYPE_USHORT,
FORMAT_TYPE_SHORT,
FORMAT_TYPE_UINT,
FORMAT_TYPE_INT,
FORMAT_TYPE_SIZE_T,
FORMAT_TYPE_PTRDIFF,
FORMAT_TYPE_NRCHARS, /* Reintroduced for this checker */
FORMAT_TYPE_FLOAT, /* for various floating point formatters */
};
struct printf_spec {
unsigned int type:8; /* format_type enum */
signed int field_width:24; /* width of output field */
unsigned int flags:8; /* flags to number() */
unsigned int base:8; /* number base, 8, 10 or 16 only */
signed int precision:16; /* # of digits/chars */
} __packed;
#define FIELD_WIDTH_MAX ((1 << 23) - 1)
#define PRECISION_MAX ((1 << 15) - 1)
extern char __check_printf_spec[1-2*(sizeof(struct printf_spec) != 8)];
static int
skip_atoi(const char **s)
{
int i = 0;
while (isdigit(**s))
i = i*10 + *((*s)++) - '0';
return i;
}
static int
format_decode(const char *fmt, struct printf_spec *spec)
{
const char *start = fmt;
char qualifier;
/* we finished early by reading the field width */
if (spec->type == FORMAT_TYPE_WIDTH) {
if (spec->field_width < 0) {
spec->field_width = -spec->field_width;
spec->flags |= LEFT;
}
spec->type = FORMAT_TYPE_NONE;
goto precision;
}
/* we finished early by reading the precision */
if (spec->type == FORMAT_TYPE_PRECISION) {
if (spec->precision < 0)
spec->precision = 0;
spec->type = FORMAT_TYPE_NONE;
goto qualifier;
}
/* By default */
spec->type = FORMAT_TYPE_NONE;
for (; *fmt ; ++fmt) {
if (*fmt == '%')
break;
}
/* Return the current non-format string */
if (fmt != start || !*fmt)
return fmt - start;
/* Process flags */
spec->flags = 0;
while (1) { /* this also skips first '%' */
bool found = true;
++fmt;
switch (*fmt) {
case '-': spec->flags |= LEFT; break;
case '+': spec->flags |= PLUS; break;
case ' ': spec->flags |= SPACE; break;
case '#': spec->flags |= SPECIAL; break;
case '0': spec->flags |= ZEROPAD; break;
default: found = false;
}
if (!found)
break;
}
/* get field width */
spec->field_width = -1;
if (isdigit(*fmt))
spec->field_width = skip_atoi(&fmt);
else if (*fmt == '*') {
/* it's the next argument */
spec->type = FORMAT_TYPE_WIDTH;
return ++fmt - start;
}
precision:
/* get the precision */
spec->precision = -1;
if (*fmt == '.') {
++fmt;
if (isdigit(*fmt)) {
spec->precision = skip_atoi(&fmt);
if (spec->precision < 0)
spec->precision = 0;
} else if (*fmt == '*') {
/* it's the next argument */
spec->type = FORMAT_TYPE_PRECISION;
return ++fmt - start;
}
}
qualifier:
/* get the conversion qualifier */
qualifier = 0;
if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
_tolower(*fmt) == 'z' || *fmt == 't') {
qualifier = *fmt++;
if (qualifier == *fmt) {
if (qualifier == 'l') {
qualifier = 'L';
++fmt;
} else if (qualifier == 'h') {
qualifier = 'H';
++fmt;
} else {
sm_warning("invalid repeated qualifier '%c'", *fmt);
}
}
}
/* default base */
spec->base = 10;
switch (*fmt) {
case 'c':
if (qualifier)
sm_warning("qualifier '%c' ignored for %%c specifier", qualifier);
spec->type = FORMAT_TYPE_CHAR;
return ++fmt - start;
case 's':
if (qualifier && qualifier != 'l')
sm_warning("qualifier '%c' ignored for %%s specifier", qualifier);
spec->type = FORMAT_TYPE_STR;
return ++fmt - start;
case 'p':
spec->type = FORMAT_TYPE_PTR;
return ++fmt - start;
case '%':
spec->type = FORMAT_TYPE_PERCENT_CHAR;
return ++fmt - start;
/* integer number formats - set up the flags and "break" */
case 'o':
spec->base = 8;
break;
case 'x':
spec->flags |= SMALL;
case 'X':
spec->base = 16;
break;
case 'd':
case 'i':
spec->flags |= SIGN;
case 'u':
break;
case 'n':
spec->type = FORMAT_TYPE_NRCHARS;
return ++fmt - start;
case 'a': case 'A':
case 'e': case 'E':
case 'f': case 'F':
case 'g': case 'G':
spec->type = FORMAT_TYPE_FLOAT;
return ++fmt - start;
default:
spec->type = FORMAT_TYPE_INVALID;
/* Unlike the kernel code, we 'consume' the invalid
* character so that it can get included in the
* report. After that, we bail out. */
return ++fmt - start;
}
if (qualifier == 'L')
spec->type = FORMAT_TYPE_LONG_LONG;
else if (qualifier == 'l') {
if (spec->flags & SIGN)
spec->type = FORMAT_TYPE_LONG;
else
spec->type = FORMAT_TYPE_ULONG;
} else if (_tolower(qualifier) == 'z') {
spec->type = FORMAT_TYPE_SIZE_T;
} else if (qualifier == 't') {
spec->type = FORMAT_TYPE_PTRDIFF;
} else if (qualifier == 'H') {
if (spec->flags & SIGN)
spec->type = FORMAT_TYPE_BYTE;
else
spec->type = FORMAT_TYPE_UBYTE;
} else if (qualifier == 'h') {
if (spec->flags & SIGN)
spec->type = FORMAT_TYPE_SHORT;
else
spec->type = FORMAT_TYPE_USHORT;
} else {
if (spec->flags & SIGN)
spec->type = FORMAT_TYPE_INT;
else
spec->type = FORMAT_TYPE_UINT;
}
return ++fmt - start;
}
static int is_struct_tag(struct symbol *type, const char *tag)
{
return type->type == SYM_STRUCT && type->ident && !strcmp(type->ident->name, tag);
}
static int has_struct_tag(struct symbol *type, const char *tag)
{
struct symbol *tmp;
if (type->type == SYM_STRUCT)
return is_struct_tag(type, tag);
if (type->type == SYM_UNION) {
FOR_EACH_PTR(type->symbol_list, tmp) {
tmp = get_real_base_type(tmp);
if (tmp && is_struct_tag(tmp, tag))
return 1;
} END_FOR_EACH_PTR(tmp);
}
return 0;
}
static int is_char_type(struct symbol *type)
{
return type == &uchar_ctype || type == &char_ctype || type == &schar_ctype;
}
/*
* I have absolutely no idea if this is how one is supposed to get the
* symbol representing a typedef, but it seems to work.
*/
struct typedef_lookup {
const char *name;
struct symbol *sym;
int failed;
};
static struct symbol *_typedef_lookup(const char *name)
{
struct ident *id;
struct symbol *node;
id = built_in_ident(name);
if (!id)
return NULL;
node = lookup_symbol(id, NS_TYPEDEF);
if (!node || node->type != SYM_NODE)
return NULL;
return get_real_base_type(node);
}
static void typedef_lookup(struct typedef_lookup *tl)
{
if (tl->sym || tl->failed)
return;
tl->sym = _typedef_lookup(tl->name);
if (!tl->sym) {
sm_perror(" could not find typedef '%s'", tl->name);
tl->failed = 1;
}
}
static void ip4(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
enum { ENDIAN_BIG, ENDIAN_LITTLE, ENDIAN_HOST } endian = ENDIAN_BIG;
assert(fmt[0] == 'i' || fmt[0] == 'I');
assert(fmt[1] == '4');
if (isalnum(fmt[2])) {
switch (fmt[2]) {
case 'h':
endian = ENDIAN_HOST;
break;
case 'l':
endian = ENDIAN_LITTLE;
break;
case 'n':
case 'b':
endian = ENDIAN_BIG;
break;
default:
sm_warning("'%%p%c4' can only be followed by one of [hnbl], not '%c'", fmt[0], fmt[2]);
}
if (isalnum(fmt[3]))
sm_warning("'%%p%c4' can only be followed by precisely one of [hnbl]", fmt[0]);
}
if (type->ctype.modifiers & MOD_NODEREF)
sm_error("passing __user pointer to '%%p%c4'", fmt[0]);
/*
* If we have a pointer to char/u8/s8, we expect the caller to
* handle endianness; I don't think there's anything we can
* do. I'd like to check that if we're passed a pointer to a
* __bitwise u32 (most likely a __be32), we should have endian
* == ENDIAN_BIG. But I can't figure out how to get that
* information (it also seems to require ensuring certain
* macros are defined). But struct in_addr certainly consists
* of only a single __be32, so in that case we can do a check.
*/
if (is_char_type(basetype))
return;
if (is_struct_tag(basetype, "in_addr") && endian != ENDIAN_BIG)
sm_warning("passing struct in_addr* to '%%p%c4%c', is the endianness ok?", fmt[0], fmt[2]);
/* ... */
}
static void ip6(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
assert(fmt[0] == 'i' || fmt[0] == 'I');
assert(fmt[1] == '6');
if (isalnum(fmt[2])) {
if (fmt[2] != 'c')
sm_warning("'%%p%c6' can only be followed by c", fmt[0]);
else if (fmt[0] == 'i')
sm_warning("'%%pi6' does not allow flag c");
if (isalnum(fmt[3]))
sm_warning("'%%p%c6%c' cannot be followed by other alphanumerics", fmt[0], fmt[2]);
}
if (type->ctype.modifiers & MOD_NODEREF)
sm_error("passing __user pointer to '%%p%c6'", fmt[0]);
}
static void ipS(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
const char *f;
assert(tolower(fmt[0]) == 'i');
assert(fmt[1] == 'S');
for (f = fmt+2; isalnum(*f); ++f) {
/* It's probably too anal checking for duplicate flags. */
if (!strchr("pfschnbl", *f))
sm_warning("'%%p%cS' cannot be followed by '%c'", fmt[0], *f);
}
/*
* XXX: Should we also allow passing a pointer to a union, one
* member of which is a struct sockaddr? It may be slightly
* cleaner actually passing &u.raw instead of just &u, though
* the generated code is of course exactly the same. For now,
* we do accept struct sockaddr_in and struct sockaddr_in6,
* since those are easy to handle and rather harmless.
*/
if (!has_struct_tag(basetype, "sockaddr") &&
!has_struct_tag(basetype, "sockaddr_in") &&
!has_struct_tag(basetype, "sockaddr_in6") &&
!has_struct_tag(basetype, "__kernel_sockaddr_storage"))
sm_error("'%%p%cS' expects argument of type struct sockaddr *, "
"argument %d has type '%s'", fmt[0], vaidx, type_to_str(type));
}
static void hex_string(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
assert(fmt[0] == 'h');
if (isalnum(fmt[1])) {
if (!strchr("CDN", fmt[1]))
sm_warning("'%%ph' cannot be followed by '%c'", fmt[1]);
if (isalnum(fmt[2]))
sm_warning("'%%ph' can be followed by at most one of [CDN], and no other alphanumerics");
}
if (type->ctype.modifiers & MOD_NODEREF)
sm_error("passing __user pointer to %%ph");
}
static void escaped_string(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
assert(fmt[0] == 'E');
while (isalnum(*++fmt)) {
if (!strchr("achnops", *fmt))
sm_warning("%%pE can only be followed by a combination of [achnops]");
}
if (type->ctype.modifiers & MOD_NODEREF)
sm_error("passing __user pointer to %%pE");
}
static void resource_string(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
assert(tolower(fmt[0]) == 'r');
if (!is_struct_tag(basetype, "resource")) {
sm_error("'%%p%c' expects argument of type struct resource *, "
"but argument %d has type '%s'", fmt[0], vaidx, type_to_str(type));
}
if (isalnum(fmt[1]))
sm_warning("'%%p%c' cannot be followed by '%c'", fmt[0], fmt[1]);
}
static void mac_address_string(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
assert(tolower(fmt[0]) == 'm');
if (isalnum(fmt[1])) {
if (!(fmt[1] == 'F' || fmt[1] == 'R'))
sm_warning("'%%p%c' cannot be followed by '%c'", fmt[0], fmt[1]);
if (fmt[0] == 'm' && fmt[1] == 'F')
sm_warning("it is pointless to pass flag F to %%pm");
if (isalnum(fmt[2]))
sm_warning("'%%p%c%c' cannot be followed by other alphanumeric", fmt[0], fmt[1]);
}
/* Technically, bdaddr_t is a typedef for an anonymous struct, but this still seems to work. */
if (!is_char_type(basetype) && !is_struct_tag(basetype, "bdaddr_t") && basetype != &void_ctype) {
sm_warning("'%%p%c' expects argument of type u8 * or bdaddr_t *, argument %d has type '%s'",
fmt[0], vaidx, type_to_str(type));
}
if (type->ctype.modifiers & MOD_NODEREF)
sm_error("passing __user pointer to '%%p%c'", fmt[0]);
}
static void dentry_file(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
const char *tag;
assert(tolower(fmt[0]) == 'd');
tag = fmt[0] == 'd' ? "dentry" : "file";
if (isalnum(fmt[1])) {
if (!strchr("234", fmt[1]))
sm_warning("'%%p%c' can only be followed by one of [234]", fmt[0]);
if (isalnum(fmt[2]))
sm_warning("'%%p%c%c' cannot be followed by '%c'", fmt[0], fmt[1], fmt[2]);
}
if (!is_struct_tag(basetype, tag))
sm_error("'%%p%c' expects argument of type struct '%s*', argument %d has type '%s'",
fmt[0], tag, vaidx, type_to_str(type));
}
static void time_and_date(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
assert(tolower(fmt[0]) == 't');
if (fmt[1] == 'R' && !is_struct_tag(basetype, "rtc_time"))
sm_error("'%%ptR' expects argument of type struct 'rtc_time', argument %d has type '%s'",
vaidx, type_to_str(type));
}
static void check_clock(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
assert(fmt[0] == 'C');
if (isalnum(fmt[1])) {
if (!strchr("nr", fmt[1]))
sm_warning("'%%pC' can only be followed by one of [nr]");
if (isalnum(fmt[2]))
sm_warning("'%%pC%c' cannot be followed by '%c'", fmt[1], fmt[2]);
}
if (!is_struct_tag(basetype, "clk"))
sm_error("'%%pC' expects argument of type 'struct clk*', argument %d has type '%s'",
vaidx, type_to_str(type));
}
static void va_format(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
assert(fmt[0] == 'V');
if (isalnum(fmt[1]))
sm_warning("%%pV cannot be followed by any alphanumerics");
if (!is_struct_tag(basetype, "va_format"))
sm_error("%%pV expects argument of type struct va_format*, argument %d has type '%s'", vaidx, type_to_str(type));
}
static void netdev_feature(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
static struct typedef_lookup netdev = { .name = "netdev_features_t" };
assert(fmt[0] == 'N');
if (fmt[1] != 'F') {
sm_error("%%pN must be followed by 'F'");
return;
}
if (isalnum(fmt[2]))
sm_warning("%%pNF cannot be followed by '%c'", fmt[2]);
typedef_lookup(&netdev);
if (!netdev.sym)
return;
if (basetype != netdev.sym)
sm_error("%%pNF expects argument of type netdev_features_t*, argument %d has type '%s'",
vaidx, type_to_str(type));
}
static void address_val(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
static struct typedef_lookup dma = { .name = "dma_addr_t" };
static struct typedef_lookup phys = { .name = "phys_addr_t" };
struct typedef_lookup *which = &phys;
const char *suf = "";
assert(fmt[0] == 'a');
if (isalnum(fmt[1])) {
switch (fmt[1]) {
case 'd':
which = &dma;
suf = "d";
break;
case 'p':
suf = "p";
break;
default:
sm_error("'%%pa' can only be followed by one of [dp]");
}
if (isalnum(fmt[2]))
sm_error("'%%pa%c' cannot be followed by '%c'", fmt[1], fmt[2]);
}
typedef_lookup(which);
if (!which->sym)
return;
if (basetype != which->sym) {
sm_error("'%%pa%s' expects argument of type '%s*', argument %d has type '%s'",
suf, which->name, vaidx, type_to_str(type));
}
}
static void block_device(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
const char *tag = "block_device";
assert(fmt[0] == 'g');
if (isalnum(fmt[1])) {
sm_warning("%%pg cannot be followed by '%c'", fmt[1]);
}
if (!is_struct_tag(basetype, tag))
sm_error("'%%p%c' expects argument of type struct '%s*', argument %d has type '%s'",
fmt[0], tag, vaidx, type_to_str(type));
}
static void flag_string(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
static struct typedef_lookup gfp = { .name = "gfp_t" };
assert(fmt[0] == 'G');
if (!isalnum(fmt[1])) {
sm_error("%%pG must be followed by one of [gpv]");
return;
}
switch (fmt[1]) {
case 'p':
case 'v':
if (basetype != &ulong_ctype)
sm_error("'%%pG%c' expects argument of type 'unsigned long *', argument %d has type '%s'",
fmt[1], vaidx, type_to_str(type));
break;
case 'g':
typedef_lookup(&gfp);
if (basetype != gfp.sym)
sm_error("'%%pGg' expects argument of type 'gfp_t *', argument %d has type '%s'",
vaidx, type_to_str(type));
break;
default:
sm_error("'%%pG' must be followed by one of [gpv]");
}
}
static void device_node_string(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
if (fmt[1] != 'F') {
sm_error("%%pO can only be followed by 'F'");
return;
}
if (!is_struct_tag(basetype, "device_node"))
sm_error("'%%pOF' expects argument of type 'struct device_node*', argument %d has type '%s'",
vaidx, type_to_str(type));
}
static void fourcc_string(const char *fmt, struct symbol *type, struct symbol *basetype, int vaidx)
{
if (fmt[1] != 'c') {
sm_error("%%p4 can only be followed by 'c'");
return;
}
if (fmt[2] == 'h' || fmt[2] == 'r' || fmt[2] == 'l' ||
fmt[2] == 'b' || fmt[2] == 'c')
return;
sm_error("'%%p4c' must be followed by one of [hrlbc]");
}
static void
pointer(const char *fmt, struct expression *arg, int vaidx)
{
struct symbol *type, *basetype;
type = get_type(arg);
if (!type) {
sm_warning("could not determine type of argument %d", vaidx);
return;
}
if (!is_ptr_type(type)) {
sm_error("%%p expects pointer argument, but argument %d has type '%s'",
vaidx, type_to_str(type));
return;
}
/* error pointers */
if (*fmt == 'e')
fmt++;
/* Just plain %p, nothing to check. */
if (!isalnum(*fmt))
return;
basetype = get_real_base_type(type);
if (is_void_type(basetype))
return;
/*
* Passing a pointer-to-array is harmless, but most likely one
* meant to pass pointer-to-first-element. If basetype is
* array type, we issue a notice and "dereference" the types
* once more.
*/
if (basetype->type == SYM_ARRAY) {
spam("note: passing pointer-to-array; is the address-of redundant?");
type = basetype;
basetype = get_real_base_type(type);
}
/*
* We pass both the type and the basetype to the helpers. If,
* for example, the pointer is really a decayed array which is
* passed to %pI4, we might want to check that it is in fact
* an array of four bytes. But most are probably only
* interested in whether the basetype makes sense. Also, the
* pointer may carry some annotation such as __user which
* might be worth checking in the handlers which actually
* dereference the pointer.
*/
switch (*fmt) {
case 'b':
case 'F':
case 'f':
case 'S':
case 's':
case 'B':
/* Can we do anything sensible? Check that the arg is a function pointer, for example? */
break;
case 'R':
case 'r':
resource_string(fmt, type, basetype, vaidx);
break;
case 'M':
case 'm':
mac_address_string(fmt, type, basetype, vaidx);
break;
case 'I':
case 'i':
switch (fmt[1]) {
case '4':
ip4(fmt, type, basetype, vaidx);
break;
case '6':
ip6(fmt, type, basetype, vaidx);
break;
case 'S':
ipS(fmt, type, basetype, vaidx);
break;
default:
sm_warning("'%%p%c' must be followed by one of [46S]", fmt[0]);
break;
}
break;
/*
* %pE and %ph can handle any valid pointer. We still check
* whether all the subsequent alphanumerics are valid for the
* particular %pX conversion.
*/
case 'E':
escaped_string(fmt, type, basetype, vaidx);
break;
case 'h':
hex_string(fmt, type, basetype, vaidx);
break;
case 'U': /* TODO */
break;
case 'V':
va_format(fmt, type, basetype, vaidx);
break;
case 'K': /* TODO */
break;
case 'N':
netdev_feature(fmt, type, basetype, vaidx);
break;
case 'a':
address_val(fmt, type, basetype, vaidx);
break;
case 'D':
case 'd':
dentry_file(fmt, type, basetype, vaidx);
break;
case 't':
time_and_date(fmt, type, basetype, vaidx);
break;
case 'C':
check_clock(fmt, type, basetype, vaidx);
break;
case 'g':
block_device(fmt, type, basetype, vaidx);
break;
case 'G':
flag_string(fmt, type, basetype, vaidx);
break;
case 'O':
device_node_string(fmt, type, basetype, vaidx);
break;
case 'x':
/* 'x' is for an unhashed pointer */
break;
case '4':
fourcc_string(fmt, type, basetype, vaidx);
break;
default:
sm_error("unrecognized %%p extension '%c', treated as normal %%p", *fmt);
}
}
/*
* A common error is to pass a "char" or "signed char" to %02x (or
* %.2X or some other variant). This can actually be a security
* problem, because a lot of code expects this to produce exactly two
* characters of output. Unfortunately this also produces false
* positives, since we're sometimes in arch-specific code on an arch
* where char is always unsigned.
*/
static void
hexbyte(const char *fmt, int fmt_len, struct expression *arg, int vaidx, struct printf_spec spec)
{
struct symbol *type;
/*
* For now, just check the most common and obvious, which is
* roughly %[.0]2[xX].
*/
if (spec.field_width != 2 && spec.precision != 2)
return;
if (spec.base != 16)
return;
type = get_type(arg);
if (!type) {
sm_warning("could not determine type of argument %d", vaidx);
return;
}
if (type_signed(type) && (type == &char_ctype || type == &schar_ctype))
sm_warning("argument %d to %.*s specifier has type '%s'",
vaidx, fmt_len, fmt, type_to_str(type));
}
static int
check_format_string(const char *fmt, const char *caller)
{
const char *f;
for (f = fmt; *f; ++f) {
unsigned char c = *f;
switch (c) {
case KERN_SOH_ASCII:
/*
* This typically arises from bad conversion
* to pr_*, e.g. pr_warn(KERN_WARNING "something").
*/
if (f != fmt)
sm_warning("KERN_* level not at start of string");
/*
* In a very few cases, the level is actually
* computed and passed via %c, as in KERN_SOH
* "%c...". printk explicitly supports
* this.
*/
if (!(('0' <= f[1] && f[1] <= '7') ||
f[1] == 'd' || /* KERN_DEFAULT */
f[1] == 'c' || /* KERN_CONT */
(f[1] == '%' && f[2] == 'c')))
sm_warning("invalid KERN_* level: KERN_SOH_ASCII followed by '\\x%02x'", (unsigned char)f[1]);
break;
case '\t':
case '\n':
case '\r':
case 0x20 ... 0x7e:
break;
case 0x80 ... 0xff:
sm_warning("format string contains non-ascii character '\\x%02x'", c);
break;
case 0x08:
if (f == fmt)
break;
/* fall through */
default:
sm_warning("format string contains unusual character '\\x%02x'", c);
break;
}
}
f = strstr(fmt, caller);
if (f && strstr(f+1, caller))
sm_warning("format string contains name of enclosing function '%s' twice", caller);
return f != NULL;
}
static int arg_is___func__(struct expression *arg)
{
if (arg->type != EXPR_SYMBOL)
return 0;
return !strcmp(arg->symbol_name->name, "__func__") ||
!strcmp(arg->symbol_name->name, "__FUNCTION__") ||
!strcmp(arg->symbol_name->name, "__PRETTY_FUNCTION__");
}
static int arg_contains_caller(struct expression *arg, const char *caller)
{
if (arg->type != EXPR_STRING)
return 0;
return strstr(arg->string->data, caller) != NULL;
}
static int is_array_of_const_char(struct symbol *sym)
{
struct symbol *base = sym->ctype.base_type;
if (base->type != SYM_ARRAY)
return 0;
if (!(base->ctype.modifiers & MOD_CONST))
return 0;
if (!is_char_type(base->ctype.base_type)) {
spam("weird: format argument is array of const '%s'", type_to_str(base->ctype.base_type));
return 0;
}
return 1;
}
static int is_const_pointer_to_const_char(struct symbol *sym)
{
struct symbol *base = sym->ctype.base_type;
if (!(sym->ctype.modifiers & MOD_CONST))
return 0;
if (base->type != SYM_PTR)
return 0;
if (!(base->ctype.modifiers & MOD_CONST))
return 0;
if (!is_char_type(base->ctype.base_type)) {
spam("weird: format argument is pointer to const '%s'", type_to_str(base->ctype.base_type));
return 0;
}
return 1;
}
static int unknown_format(struct expression *expr)
{
struct state_list *slist;
slist = get_strings(expr);
if (!slist)
return 1;
if (slist_has_state(slist, &undefined))
return 1;
free_slist(&slist);
return 0;
}
static bool has_hex_prefix(const char *orig_fmt, const char *old_fmt)
{
return old_fmt >= orig_fmt + 2 &&
old_fmt[-2] == '0' && _tolower(old_fmt[-1]) == 'x';
}
static bool is_integer_specifier(int type)
{
switch (type) {
case FORMAT_TYPE_LONG_LONG:
case FORMAT_TYPE_ULONG:
case FORMAT_TYPE_LONG:
case FORMAT_TYPE_UBYTE:
case FORMAT_TYPE_BYTE:
case FORMAT_TYPE_USHORT:
case FORMAT_TYPE_SHORT:
case FORMAT_TYPE_UINT:
case FORMAT_TYPE_INT:
case FORMAT_TYPE_SIZE_T:
case FORMAT_TYPE_PTRDIFF:
return true;
default:
return false;
}
}
static int
is_cast_expr(struct expression *expr)
{
if (!expr)
return 0;
switch (expr->type) {
case EXPR_CAST:
case EXPR_FORCE_CAST:
/* not EXPR_IMPLIED_CAST for our purposes */
return 1;
default:
return 0;
}
}
static void
check_cast_from_pointer(const char *fmt, int len, struct expression *arg, int va_idx)