1 //= UnixAPIChecker.h - Checks preconditions for various Unix APIs --*- C++ -*-//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This defines UnixAPIChecker, which is an assortment of checks on calls
10 // to various, widely used UNIX/Posix functions.
12 //===----------------------------------------------------------------------===//
14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15 #include "clang/Basic/TargetInfo.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/Support/raw_ostream.h"
26 using namespace clang
;
29 enum class OpenVariant
{
30 /// The standard open() call:
31 /// int open(const char *path, int oflag, ...);
34 /// The variant taking a directory file descriptor and a relative path:
35 /// int openat(int fd, const char *path, int oflag, ...);
41 class UnixAPIMisuseChecker
: public Checker
< check::PreStmt
<CallExpr
> > {
42 mutable std::unique_ptr
<BugType
> BT_open
, BT_pthreadOnce
;
43 mutable std::optional
<uint64_t> Val_O_CREAT
;
46 void checkPreStmt(const CallExpr
*CE
, CheckerContext
&C
) const;
48 void CheckOpen(CheckerContext
&C
, const CallExpr
*CE
) const;
49 void CheckOpenAt(CheckerContext
&C
, const CallExpr
*CE
) const;
50 void CheckPthreadOnce(CheckerContext
&C
, const CallExpr
*CE
) const;
52 void CheckOpenVariant(CheckerContext
&C
,
53 const CallExpr
*CE
, OpenVariant Variant
) const;
55 void ReportOpenBug(CheckerContext
&C
,
56 ProgramStateRef State
,
58 SourceRange SR
) const;
62 class UnixAPIPortabilityChecker
: public Checker
< check::PreStmt
<CallExpr
> > {
64 void checkPreStmt(const CallExpr
*CE
, CheckerContext
&C
) const;
67 mutable std::unique_ptr
<BugType
> BT_mallocZero
;
69 void CheckCallocZero(CheckerContext
&C
, const CallExpr
*CE
) const;
70 void CheckMallocZero(CheckerContext
&C
, const CallExpr
*CE
) const;
71 void CheckReallocZero(CheckerContext
&C
, const CallExpr
*CE
) const;
72 void CheckReallocfZero(CheckerContext
&C
, const CallExpr
*CE
) const;
73 void CheckAllocaZero(CheckerContext
&C
, const CallExpr
*CE
) const;
74 void CheckAllocaWithAlignZero(CheckerContext
&C
, const CallExpr
*CE
) const;
75 void CheckVallocZero(CheckerContext
&C
, const CallExpr
*CE
) const;
77 bool ReportZeroByteAllocation(CheckerContext
&C
,
78 ProgramStateRef falseState
,
80 const char *fn_name
) const;
81 void BasicAllocationCheck(CheckerContext
&C
,
83 const unsigned numArgs
,
84 const unsigned sizeArg
,
85 const char *fn
) const;
88 } //end anonymous namespace
90 static void LazyInitialize(const CheckerBase
*Checker
,
91 std::unique_ptr
<BugType
> &BT
,
95 BT
.reset(new BugType(Checker
, name
, categories::UnixAPI
));
98 //===----------------------------------------------------------------------===//
99 // "open" (man 2 open)
100 //===----------------------------------------------------------------------===/
102 void UnixAPIMisuseChecker::checkPreStmt(const CallExpr
*CE
,
103 CheckerContext
&C
) const {
104 const FunctionDecl
*FD
= C
.getCalleeDecl(CE
);
105 if (!FD
|| FD
->getKind() != Decl::Function
)
108 // Don't treat functions in namespaces with the same name a Unix function
109 // as a call to the Unix function.
110 const DeclContext
*NamespaceCtx
= FD
->getEnclosingNamespaceContext();
111 if (isa_and_nonnull
<NamespaceDecl
>(NamespaceCtx
))
114 StringRef FName
= C
.getCalleeName(FD
);
121 else if (FName
== "openat")
124 else if (FName
== "pthread_once")
125 CheckPthreadOnce(C
, CE
);
127 void UnixAPIMisuseChecker::ReportOpenBug(CheckerContext
&C
,
128 ProgramStateRef State
,
130 SourceRange SR
) const {
131 ExplodedNode
*N
= C
.generateErrorNode(State
);
135 LazyInitialize(this, BT_open
, "Improper use of 'open'");
137 auto Report
= std::make_unique
<PathSensitiveBugReport
>(*BT_open
, Msg
, N
);
138 Report
->addRange(SR
);
139 C
.emitReport(std::move(Report
));
142 void UnixAPIMisuseChecker::CheckOpen(CheckerContext
&C
,
143 const CallExpr
*CE
) const {
144 CheckOpenVariant(C
, CE
, OpenVariant::Open
);
147 void UnixAPIMisuseChecker::CheckOpenAt(CheckerContext
&C
,
148 const CallExpr
*CE
) const {
149 CheckOpenVariant(C
, CE
, OpenVariant::OpenAt
);
152 void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext
&C
,
154 OpenVariant Variant
) const {
155 // The index of the argument taking the flags open flags (O_RDONLY,
156 // O_WRONLY, O_CREAT, etc.),
157 unsigned int FlagsArgIndex
;
158 const char *VariantName
;
160 case OpenVariant::Open
:
162 VariantName
= "open";
164 case OpenVariant::OpenAt
:
166 VariantName
= "openat";
170 // All calls should at least provide arguments up to the 'flags' parameter.
171 unsigned int MinArgCount
= FlagsArgIndex
+ 1;
173 // If the flags has O_CREAT set then open/openat() require an additional
174 // argument specifying the file mode (permission bits) for the created file.
175 unsigned int CreateModeArgIndex
= FlagsArgIndex
+ 1;
177 // The create mode argument should be the last argument.
178 unsigned int MaxArgCount
= CreateModeArgIndex
+ 1;
180 ProgramStateRef state
= C
.getState();
182 if (CE
->getNumArgs() < MinArgCount
) {
183 // The frontend should issue a warning for this case. Just return.
185 } else if (CE
->getNumArgs() == MaxArgCount
) {
186 const Expr
*Arg
= CE
->getArg(CreateModeArgIndex
);
187 QualType QT
= Arg
->getType();
188 if (!QT
->isIntegerType()) {
189 SmallString
<256> SBuf
;
190 llvm::raw_svector_ostream
OS(SBuf
);
191 OS
<< "The " << CreateModeArgIndex
+ 1
192 << llvm::getOrdinalSuffix(CreateModeArgIndex
+ 1)
193 << " argument to '" << VariantName
<< "' is not an integer";
195 ReportOpenBug(C
, state
,
197 Arg
->getSourceRange());
200 } else if (CE
->getNumArgs() > MaxArgCount
) {
201 SmallString
<256> SBuf
;
202 llvm::raw_svector_ostream
OS(SBuf
);
203 OS
<< "Call to '" << VariantName
<< "' with more than " << MaxArgCount
206 ReportOpenBug(C
, state
,
208 CE
->getArg(MaxArgCount
)->getSourceRange());
212 // The definition of O_CREAT is platform specific. We need a better way
213 // of querying this information from the checking environment.
215 if (C
.getASTContext().getTargetInfo().getTriple().getVendor()
216 == llvm::Triple::Apple
)
217 Val_O_CREAT
= 0x0200;
219 // FIXME: We need a more general way of getting the O_CREAT value.
220 // We could possibly grovel through the preprocessor state, but
221 // that would require passing the Preprocessor object to the ExprEngine.
222 // See also: MallocChecker.cpp / M_ZERO.
227 // Now check if oflags has O_CREAT set.
228 const Expr
*oflagsEx
= CE
->getArg(FlagsArgIndex
);
229 const SVal V
= C
.getSVal(oflagsEx
);
230 if (!isa
<NonLoc
>(V
)) {
231 // The case where 'V' can be a location can only be due to a bad header,
232 // so in this case bail out.
235 NonLoc oflags
= V
.castAs
<NonLoc
>();
236 NonLoc ocreateFlag
= C
.getSValBuilder()
237 .makeIntVal(*Val_O_CREAT
, oflagsEx
->getType())
239 SVal maskedFlagsUC
= C
.getSValBuilder().evalBinOpNN(state
, BO_And
,
241 oflagsEx
->getType());
242 if (maskedFlagsUC
.isUnknownOrUndef())
244 DefinedSVal maskedFlags
= maskedFlagsUC
.castAs
<DefinedSVal
>();
246 // Check if maskedFlags is non-zero.
247 ProgramStateRef trueState
, falseState
;
248 std::tie(trueState
, falseState
) = state
->assume(maskedFlags
);
250 // Only emit an error if the value of 'maskedFlags' is properly
252 if (!(trueState
&& !falseState
))
255 if (CE
->getNumArgs() < MaxArgCount
) {
256 SmallString
<256> SBuf
;
257 llvm::raw_svector_ostream
OS(SBuf
);
258 OS
<< "Call to '" << VariantName
<< "' requires a "
259 << CreateModeArgIndex
+ 1
260 << llvm::getOrdinalSuffix(CreateModeArgIndex
+ 1)
261 << " argument when the 'O_CREAT' flag is set";
262 ReportOpenBug(C
, trueState
,
264 oflagsEx
->getSourceRange());
268 //===----------------------------------------------------------------------===//
270 //===----------------------------------------------------------------------===//
272 void UnixAPIMisuseChecker::CheckPthreadOnce(CheckerContext
&C
,
273 const CallExpr
*CE
) const {
275 // This is similar to 'CheckDispatchOnce' in the MacOSXAPIChecker.
276 // They can possibly be refactored.
278 if (CE
->getNumArgs() < 1)
281 // Check if the first argument is stack allocated. If so, issue a warning
282 // because that's likely to be bad news.
283 ProgramStateRef state
= C
.getState();
284 const MemRegion
*R
= C
.getSVal(CE
->getArg(0)).getAsRegion();
285 if (!R
|| !isa
<StackSpaceRegion
>(R
->getMemorySpace()))
288 ExplodedNode
*N
= C
.generateErrorNode(state
);
293 llvm::raw_svector_ostream
os(S
);
294 os
<< "Call to 'pthread_once' uses";
295 if (const VarRegion
*VR
= dyn_cast
<VarRegion
>(R
))
296 os
<< " the local variable '" << VR
->getDecl()->getName() << '\'';
298 os
<< " stack allocated memory";
299 os
<< " for the \"control\" value. Using such transient memory for "
300 "the control value is potentially dangerous.";
301 if (isa
<VarRegion
>(R
) && isa
<StackLocalsSpaceRegion
>(R
->getMemorySpace()))
302 os
<< " Perhaps you intended to declare the variable as 'static'?";
304 LazyInitialize(this, BT_pthreadOnce
, "Improper use of 'pthread_once'");
307 std::make_unique
<PathSensitiveBugReport
>(*BT_pthreadOnce
, os
.str(), N
);
308 report
->addRange(CE
->getArg(0)->getSourceRange());
309 C
.emitReport(std::move(report
));
312 //===----------------------------------------------------------------------===//
313 // "calloc", "malloc", "realloc", "reallocf", "alloca" and "valloc"
314 // with allocation size 0
315 //===----------------------------------------------------------------------===//
317 // FIXME: Eventually these should be rolled into the MallocChecker, but right now
318 // they're more basic and valuable for widespread use.
320 // Returns true if we try to do a zero byte allocation, false otherwise.
321 // Fills in trueState and falseState.
322 static bool IsZeroByteAllocation(ProgramStateRef state
,
324 ProgramStateRef
*trueState
,
325 ProgramStateRef
*falseState
) {
326 std::tie(*trueState
, *falseState
) =
327 state
->assume(argVal
.castAs
<DefinedSVal
>());
329 return (*falseState
&& !*trueState
);
332 // Generates an error report, indicating that the function whose name is given
333 // will perform a zero byte allocation.
334 // Returns false if an error occurred, true otherwise.
335 bool UnixAPIPortabilityChecker::ReportZeroByteAllocation(
337 ProgramStateRef falseState
,
339 const char *fn_name
) const {
340 ExplodedNode
*N
= C
.generateErrorNode(falseState
);
344 LazyInitialize(this, BT_mallocZero
,
345 "Undefined allocation of 0 bytes (CERT MEM04-C; CWE-131)");
348 llvm::raw_svector_ostream
os(S
);
349 os
<< "Call to '" << fn_name
<< "' has an allocation size of 0 bytes";
351 std::make_unique
<PathSensitiveBugReport
>(*BT_mallocZero
, os
.str(), N
);
353 report
->addRange(arg
->getSourceRange());
354 bugreporter::trackExpressionValue(N
, arg
, *report
);
355 C
.emitReport(std::move(report
));
360 // Does a basic check for 0-sized allocations suitable for most of the below
361 // functions (modulo "calloc")
362 void UnixAPIPortabilityChecker::BasicAllocationCheck(CheckerContext
&C
,
364 const unsigned numArgs
,
365 const unsigned sizeArg
,
366 const char *fn
) const {
367 // Check for the correct number of arguments.
368 if (CE
->getNumArgs() != numArgs
)
371 // Check if the allocation size is 0.
372 ProgramStateRef state
= C
.getState();
373 ProgramStateRef trueState
= nullptr, falseState
= nullptr;
374 const Expr
*arg
= CE
->getArg(sizeArg
);
375 SVal argVal
= C
.getSVal(arg
);
377 if (argVal
.isUnknownOrUndef())
380 // Is the value perfectly constrained to zero?
381 if (IsZeroByteAllocation(state
, argVal
, &trueState
, &falseState
)) {
382 (void) ReportZeroByteAllocation(C
, falseState
, arg
, fn
);
385 // Assume the value is non-zero going forward.
387 if (trueState
!= state
)
388 C
.addTransition(trueState
);
391 void UnixAPIPortabilityChecker::CheckCallocZero(CheckerContext
&C
,
392 const CallExpr
*CE
) const {
393 unsigned int nArgs
= CE
->getNumArgs();
397 ProgramStateRef state
= C
.getState();
398 ProgramStateRef trueState
= nullptr, falseState
= nullptr;
401 for (i
= 0; i
< nArgs
; i
++) {
402 const Expr
*arg
= CE
->getArg(i
);
403 SVal argVal
= C
.getSVal(arg
);
404 if (argVal
.isUnknownOrUndef()) {
411 if (IsZeroByteAllocation(state
, argVal
, &trueState
, &falseState
)) {
412 if (ReportZeroByteAllocation(C
, falseState
, arg
, "calloc"))
421 // Assume the value is non-zero going forward.
423 if (trueState
!= state
)
424 C
.addTransition(trueState
);
427 void UnixAPIPortabilityChecker::CheckMallocZero(CheckerContext
&C
,
428 const CallExpr
*CE
) const {
429 BasicAllocationCheck(C
, CE
, 1, 0, "malloc");
432 void UnixAPIPortabilityChecker::CheckReallocZero(CheckerContext
&C
,
433 const CallExpr
*CE
) const {
434 BasicAllocationCheck(C
, CE
, 2, 1, "realloc");
437 void UnixAPIPortabilityChecker::CheckReallocfZero(CheckerContext
&C
,
438 const CallExpr
*CE
) const {
439 BasicAllocationCheck(C
, CE
, 2, 1, "reallocf");
442 void UnixAPIPortabilityChecker::CheckAllocaZero(CheckerContext
&C
,
443 const CallExpr
*CE
) const {
444 BasicAllocationCheck(C
, CE
, 1, 0, "alloca");
447 void UnixAPIPortabilityChecker::CheckAllocaWithAlignZero(
449 const CallExpr
*CE
) const {
450 BasicAllocationCheck(C
, CE
, 2, 0, "__builtin_alloca_with_align");
453 void UnixAPIPortabilityChecker::CheckVallocZero(CheckerContext
&C
,
454 const CallExpr
*CE
) const {
455 BasicAllocationCheck(C
, CE
, 1, 0, "valloc");
458 void UnixAPIPortabilityChecker::checkPreStmt(const CallExpr
*CE
,
459 CheckerContext
&C
) const {
460 const FunctionDecl
*FD
= C
.getCalleeDecl(CE
);
461 if (!FD
|| FD
->getKind() != Decl::Function
)
464 // Don't treat functions in namespaces with the same name a Unix function
465 // as a call to the Unix function.
466 const DeclContext
*NamespaceCtx
= FD
->getEnclosingNamespaceContext();
467 if (isa_and_nonnull
<NamespaceDecl
>(NamespaceCtx
))
470 StringRef FName
= C
.getCalleeName(FD
);
474 if (FName
== "calloc")
475 CheckCallocZero(C
, CE
);
477 else if (FName
== "malloc")
478 CheckMallocZero(C
, CE
);
480 else if (FName
== "realloc")
481 CheckReallocZero(C
, CE
);
483 else if (FName
== "reallocf")
484 CheckReallocfZero(C
, CE
);
486 else if (FName
== "alloca" || FName
== "__builtin_alloca")
487 CheckAllocaZero(C
, CE
);
489 else if (FName
== "__builtin_alloca_with_align")
490 CheckAllocaWithAlignZero(C
, CE
);
492 else if (FName
== "valloc")
493 CheckVallocZero(C
, CE
);
496 //===----------------------------------------------------------------------===//
498 //===----------------------------------------------------------------------===//
500 #define REGISTER_CHECKER(CHECKERNAME) \
501 void ento::register##CHECKERNAME(CheckerManager &mgr) { \
502 mgr.registerChecker<CHECKERNAME>(); \
505 bool ento::shouldRegister##CHECKERNAME(const CheckerManager &mgr) { \
509 REGISTER_CHECKER(UnixAPIMisuseChecker
)
510 REGISTER_CHECKER(UnixAPIPortabilityChecker
)