-
Notifications
You must be signed in to change notification settings - Fork 2
/
PHPZPPChecker.cpp
493 lines (416 loc) · 16.6 KB
/
PHPZPPChecker.cpp
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
//===-- PHPZPPChecker.cpp -------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Defines a checker for proper use of PHP's zend_parse_parameters(),
// zend_parse_parameters(), zend_parse_method_parameters() and
// zend_parse_method_parameters_ex() functions.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringSwitch.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include <cassert>
using namespace clang;
using namespace ento;
namespace {
class PHPNativeType {
StringRef name;
bool hasVal;
int pointerLevel;
public:
PHPNativeType() : hasVal(false), pointerLevel(0) {}
PHPNativeType(StringRef name, int pointerLevel = 0)
: name(name), hasVal(true), pointerLevel(pointerLevel) {}
PHPNativeType(StringRef name, const char *pointerLevel)
: name(name), hasVal(true), pointerLevel(strlen(pointerLevel)) {}
StringRef getName() const {
assert(hasVal);
return name;
}
int getPointerLevel() const { return pointerLevel; }
operator bool() const { return hasVal; }
};
typedef std::vector<PHPNativeType> PHPTypeVector;
typedef std::map<char, PHPTypeVector > PHPTypeMap;
PHPTypeVector &operator<<(PHPTypeMap &map, char format) {
return map.insert(PHPTypeMap::value_type(
format, PHPTypeVector())).first->second;
}
PHPTypeVector &operator&(PHPTypeVector &vec,
const StringRef &name) {
std::pair<StringRef, StringRef> type = name.split(' ');
vec.push_back(PHPNativeType(type.first, type.second.size()));
return vec;
}
// These mappings map a zpp modifier to underlying types.
// Mind: zpp receives the address of the object to store in wich adds an
// indirection level.
// Some types return multiple values, these are added multiple times in order to
// this list (i.e. a string "s" consists of a char array and length)
static void fillMapPHPBase(PHPTypeMap &map) {
map << 'a' & "zval **";
map << 'A' & "zval **";
map << 'b' & "zend_bool *";
map << 'C' & "zend_class_entry **";
map << 'd' & "double *";
map << 'f' & "zend_fcall_info *"
& "zend_fcall_info_cache *";
map << 'h' & "HashTable **";
map << 'H' & "HashTable **";
map << 'o' & "zval **";
map << 'O' & "zval **"
& "zend_class_entry *";
map << 'r' & "zval **";
map << 'z' & "zval **";
map << '|';
map << '/';
map << '!';
map << '+' & "zval ****"
& "int *";
map << '*' & "zval ****"
& "int *";
}
static void fillMapPHP5(PHPTypeMap &map) {
fillMapPHPBase(map);
map << 'l' & "long *";
map << 'L' & "long *";
map << 'p' & "char **"
& "int *";
map << 's' & "char **"
& "int *";
map << 'Z' & "zval ***";
}
static void fillMapPHP7(PHPTypeMap &map) {
fillMapPHPBase(map);
map << 'l' & "zend_long *";
map << 'L' & "zend_long *";
map << 'P' & "zend_string **";
map << 'S' & "zend_string **";
map << 'p' & "char **"
& "size_t *";
map << 's' & "char **"
& "size_t *";
}
class PHPZPPChecker
: public Checker<check::PreCall, check::ASTDecl<TypedefDecl> > {
mutable IdentifierInfo *IIzpp, *IIzpp_ex, *IIzpmp, *IIzpmp_ex;
#if (CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR >= 5) || \
CLANG_VERSION_MAJOR > 3
std::unique_ptr<BugType> InvalidTypeBugType;
std::unique_ptr<BugType> InvalidModifierBugType;
std::unique_ptr<BugType> WrongArgumentNumberBugType;
#else
OwningPtr<BugType> InvalidTypeBugType;
OwningPtr<BugType> InvalidModifierBugType;
OwningPtr<BugType> WrongArgumentNumberBugType;
#endif
PHPTypeMap map;
typedef std::map<const StringRef, const QualType> TypedefMap;
mutable TypedefMap typedefs;
void initIdentifierInfo(ASTContext &Ctx) const;
const StringLiteral *getCStringLiteral(const SVal &val) const;
void reportInvalidType(unsigned offset, char modifier, const SVal &val,
const PHPNativeType &expectedType,
const QualType initialType, CheckerContext &C) const;
void reportInvalidIndirection(unsigned offset, char modifier, const SVal &val,
const PHPNativeType &expectedType,
int passedPointerLevel,
CheckerContext &C) const;
void reportUnknownModifier(char modifier, CheckerContext &C) const;
void reportTooFewArgs(const StringRef &format_spec, char modifier,
CheckerContext &C) const;
void reportTooManyArgs(const StringRef &format_spec, unsigned min_req,
unsigned offset, const CallEvent &Call,
CheckerContext &C) const;
void compareTypeWithSVal(unsigned offset, char modifier, const SVal &val,
const PHPNativeType &expectedType,
CheckerContext &C) const;
bool checkArgs(const StringRef &format_spec, unsigned &offset,
const unsigned numArgs, const CallEvent &Call,
CheckerContext &C) const;
public:
PHPZPPChecker();
typedef void (*MapFiller)(PHPTypeMap &);
void setMap(MapFiller filler);
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
void checkASTDecl(const TypedefDecl *td, AnalysisManager &Mgr,
BugReporter &BR) const;
};
llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const PHPNativeType &type) {
os << type.getName() << " ";
for (int i = 0; i < type.getPointerLevel(); ++i) {
os << "*";
}
return os;
}
}
static raw_ostream &debug_stream() {
#ifdef DEBUG_PHP_ZPP_CHECKER
return llvm::outs();
#else
return llvm::nulls();
#endif
}
static BugType *createZZPAPIError(StringRef name) {
return new BugType(
#if (CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR >= 5) || \
CLANG_VERSION_MAJOR > 3
new CheckerBase(),
#endif
name, "PHP ZPP API Error");
}
PHPZPPChecker::PHPZPPChecker()
: IIzpp(0), IIzpp_ex(0), IIzpmp(0), IIzpmp_ex(0) {
InvalidTypeBugType.reset(createZZPAPIError("Invalid type"));
InvalidModifierBugType.reset(createZZPAPIError("Invalid modifier"));
WrongArgumentNumberBugType.reset(createZZPAPIError("Wrong number of zpp arguments"));
}
void PHPZPPChecker::setMap(MapFiller filler) {
map.clear();
filler(map);
}
const StringLiteral *PHPZPPChecker::getCStringLiteral(const SVal &val) const {
// Copied from tools/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
// Get the memory region pointed to by the val.
const MemRegion *bufRegion = val.getAsRegion();
if (!bufRegion)
return NULL;
// Strip casts off the memory region.
bufRegion = bufRegion->StripCasts();
// Cast the memory region to a string region.
const StringRegion *strRegion = dyn_cast<StringRegion>(bufRegion);
if (!strRegion)
return NULL;
// Return the actual string in the string region.
return strRegion->getStringLiteral();
}
void PHPZPPChecker::reportInvalidType(unsigned offset, char modifier,
const SVal &val,
const PHPNativeType &expectedType,
const QualType initialType,
CheckerContext &C) const {
SmallString<256> buf;
llvm::raw_svector_ostream os(buf);
os << "Type of passed argument ";
val.dumpToStream(os);
os << " is of type " << initialType.getAsString()
<< " which did not match expected " << expectedType << " for modifier '"
<< modifier << "' at offset " << offset + 1 << ".";
BugReport *R =
new BugReport(*InvalidTypeBugType, os.str(), C.addTransition());
R->markInteresting(val);
C.emitReport(R);
}
void PHPZPPChecker::reportInvalidIndirection(unsigned offset, char modifier,
const SVal &val,
const PHPNativeType &expectedType,
int passedPointerLevel,
CheckerContext &C) const {
SmallString<256> buf;
llvm::raw_svector_ostream os(buf);
os << "Pointer indirection level of passed argument ";
val.dumpToStream(os);
os << " is " << passedPointerLevel << " which did not match expected "
<< expectedType.getPointerLevel() << " of " << expectedType
<< " for modifier '" << modifier << "' at offset " << offset + 1 << ".";
BugReport *R =
new BugReport(*InvalidTypeBugType, os.str(), C.addTransition());
R->markInteresting(val);
C.emitReport(R);
}
void PHPZPPChecker::reportTooFewArgs(const StringRef &format_spec,
char modifier, CheckerContext &C) const {
SmallString<255> buf;
llvm::raw_svector_ostream os(buf);
os << "Too few arguments for format \"" << format_spec
<< "\" while checking for modifier '" << modifier << "'.";
BugReport *R =
new BugReport(*WrongArgumentNumberBugType, os.str(), C.addTransition());
C.emitReport(R);
}
void PHPZPPChecker::reportUnknownModifier(char modifier,
CheckerContext &C) const {
SmallString<32> buf;
llvm::raw_svector_ostream os(buf);
os << "Unknown modifier '" << modifier << "'";
BugReport *R =
new BugReport(*InvalidModifierBugType, os.str(), C.addTransition());
C.emitReport(R);
}
void PHPZPPChecker::reportTooManyArgs(const StringRef &format_spec,
unsigned min_req, unsigned offset,
const CallEvent &Call,
CheckerContext &C) const {
SmallString<32> buf;
llvm::raw_svector_ostream os(buf);
os << "Too many arguments, modifier \"" << format_spec << "\" requires "
<< offset - min_req + 1 << " arguments.";
BugReport *R =
new BugReport(*WrongArgumentNumberBugType, os.str(), C.addTransition());
R->markInteresting(Call.getArgSVal(offset));
C.emitReport(R);
}
static const QualType getQualTypeForSVal(const SVal &val) {
const MemRegion *region = val.getAsRegion();
if (!region) {
// TODO: pdo_dbh.c uses zpp(0 TSRMLS_CC, "z|z", NULL, NULL) to report an error
// Should we report an error for such a construct? In other situations it could be wrong
// For this specific case we could mitigate by checking whether the first argument (ht)
// is 0 as it is hard coded in this case. Right now we report no error if NULL is passed.
return QualType();
}
if (isa<TypedValueRegion>(region)) {
return cast<TypedValueRegion>(region)->getLocationType();
}
if (isa<SymbolicRegion>(region)) {
return cast<SymbolicRegion>(region)
->getSymbol()
->getType();
}
return QualType();
}
void PHPZPPChecker::compareTypeWithSVal(unsigned offset, char modifier, const SVal &val,
const PHPNativeType &expectedType,
CheckerContext &C) const {
const QualType initialType = getQualTypeForSVal(val);
if (initialType.isNull()) {
// TODO need a good way to report this, even though this is no error
llvm::outs() << "Couldn't get type for argument at offset " << offset << "\n";
val.dump();
return;
}
QualType type = initialType.getCanonicalType();
int passedPointerLevel = 0;
while (type->isPointerType()) {
++passedPointerLevel;
type = type->getPointeeType();
}
bool match = false;
const TypedefMap::const_iterator typedef_ = typedefs.find(expectedType.getName());
if (typedef_ != typedefs.end()) {
match = (type == typedef_->second.getCanonicalType());
} else {
match = (type.getAsString() == expectedType.getName());
}
if (!match) {
reportInvalidType(offset, modifier, val, expectedType, initialType, C);
return;
}
if (passedPointerLevel != expectedType.getPointerLevel()) {
reportInvalidIndirection(offset, modifier, val, expectedType, passedPointerLevel, C);
return;
}
return;
}
bool PHPZPPChecker::checkArgs(const StringRef &format_spec, unsigned &offset,
const unsigned numArgs, const CallEvent &Call,
CheckerContext &C) const {
Call.dump(debug_stream());
for (StringRef::const_iterator modifier = format_spec.begin(),
last_mod = format_spec.end();
modifier != last_mod; ++modifier) {
debug_stream() << " I am checking for " << *modifier << "\n";
const PHPTypeMap::const_iterator it = map.find(*modifier);
if (it == map.end()) {
reportUnknownModifier(*modifier, C);
return false;
}
for (PHPTypeVector::const_iterator type = it->second.begin();
type != it->second.end(); ++type) {
++offset;
debug_stream() << " I need a " << *type << " (" << offset << ")\n";
if (numArgs <= offset) {
reportTooFewArgs(format_spec, *modifier, C);
debug_stream() << "!!!!I am missing args! " << numArgs << "<=" << offset << "\n";
return false;
}
const SVal val = Call.getArgSVal(offset);
compareTypeWithSVal(offset, *modifier, val, *type, C);
// Even if there is a type mismatch we can continue, most of the time
// this should be a simple mistake by the user, in rare cases the user
// missed an argument and will get many subsequent errors
}
}
return true;
}
void PHPZPPChecker::checkPreCall(const CallEvent &Call,
CheckerContext &C) const {
initIdentifierInfo(C.getASTContext());
if (!Call.isGlobalCFunction())
return;
const IdentifierInfo *callee = Call.getCalleeIdentifier();
if (callee != IIzpp && callee && IIzpp_ex && callee != IIzpmp &&
callee != IIzpmp_ex) {
return;
}
const FunctionDecl *decl = cast<FunctionDecl>(Call.getDecl());
// we want the offset to be the last required argument which is the format
// string, offset is 0-based, thus -1
unsigned offset = decl->getMinRequiredArguments() - 1;
const unsigned numArgs = Call.getNumArgs();
if (numArgs <= offset)
// Something is really weird - this should be caught by the compiler
return;
const StringLiteral *format_spec_sl =
getCStringLiteral(Call.getArgSVal(offset));
if (!format_spec_sl) {
// TODO need a good way to report this, even though this is no error
llvm::outs() << "Couldn't get format string looked at offset " << offset << "\n";
Call.dump();
return;
}
const StringRef format_spec = format_spec_sl->getBytes();
// All preconditions met, we can do the actual check \o/
if (!checkArgs(format_spec, offset, numArgs, Call, C))
return;
if (numArgs > 1 + offset) {
reportTooManyArgs(format_spec, decl->getMinRequiredArguments(), offset, Call, C);
}
}
void PHPZPPChecker::checkASTDecl(const TypedefDecl *td, AnalysisManager &Mgr, BugReporter &BR) const {
// This extra map might be quite inefficient, probably we should iterat over the delarations, later in
// the check?
// for (DeclContext::decl_iterator it = decl->getParent()->decls_begin(); it != decl->getParent()->decls_end(); ++it) { if (isa<TypedefDecl>(*it)) {
typedefs.insert(TypedefMap::value_type(td->getName(), td->getUnderlyingType()));
}
void PHPZPPChecker::initIdentifierInfo(ASTContext &Ctx) const {
if (IIzpp)
return;
IIzpp = &Ctx.Idents.get("zend_parse_parameters");
IIzpp_ex = &Ctx.Idents.get("zend_parse_parameters_ex");
IIzpmp = &Ctx.Idents.get("zend_parse_method_parameters");
IIzpmp_ex = &Ctx.Idents.get("zend_parse_method_parameters_ex");
}
static void initPHPChecker(CheckerManager &mgr) {
const char *version = getenv("PHP_ZPP_CHECKER_VERSION");
if (!version) {
version = "PHP5";
}
PHPZPPChecker *checker = mgr.registerChecker<PHPZPPChecker>();
PHPZPPChecker::MapFiller filler =
llvm::StringSwitch<PHPZPPChecker::MapFiller>(
mgr.getAnalyzerOptions()
.Config.GetOrCreateValue("php-zpp-version", version)
.getValue())
.Case("PHP5", fillMapPHP5)
.Case("PHP7", fillMapPHP7)
.Default(NULL);
if (filler) {
checker->setMap(filler);
}
}
extern "C" void clang_registerCheckers(CheckerRegistry ®istry) {
registry.addChecker(initPHPChecker, "php.ZPPChecker",
"Check zend_parse_parameters usage");
}
extern "C" const char clang_analyzerAPIVersionString[] =
CLANG_ANALYZER_API_VERSION_STRING;