xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/PutenvStackArrayChecker.cpp (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 //== PutenvStackArrayChecker.cpp ------------------------------- -*- C++ -*--=//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines PutenvStackArrayChecker which finds calls of ``putenv``
10 // function with automatic array variable as the argument.
11 // https://wiki.sei.cmu.edu/confluence/x/6NYxBQ
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "AllocationState.h"
16 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
24 
25 using namespace clang;
26 using namespace ento;
27 
28 namespace {
29 class PutenvStackArrayChecker : public Checker<check::PostCall> {
30 private:
31   BugType BT{this, "'putenv' called with stack-allocated string",
32              categories::SecurityError};
33   const CallDescription Putenv{CDM::CLibrary, {"putenv"}, 1};
34 
35 public:
36   void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
37 };
38 } // namespace
39 
40 void PutenvStackArrayChecker::checkPostCall(const CallEvent &Call,
41                                             CheckerContext &C) const {
42   if (!Putenv.matches(Call))
43     return;
44 
45   SVal ArgV = Call.getArgSVal(0);
46   const Expr *ArgExpr = Call.getArgExpr(0);
47 
48   const auto *SSR =
49       dyn_cast<StackSpaceRegion>(ArgV.getAsRegion()->getMemorySpace());
50   if (!SSR)
51     return;
52   const auto *StackFrameFuncD =
53       dyn_cast_or_null<FunctionDecl>(SSR->getStackFrame()->getDecl());
54   if (StackFrameFuncD && StackFrameFuncD->isMain())
55     return;
56 
57   StringRef ErrorMsg = "The 'putenv' function should not be called with "
58                        "arrays that have automatic storage";
59   ExplodedNode *N = C.generateErrorNode();
60   auto Report = std::make_unique<PathSensitiveBugReport>(BT, ErrorMsg, N);
61 
62   // Track the argument.
63   bugreporter::trackExpressionValue(Report->getErrorNode(), ArgExpr, *Report);
64 
65   C.emitReport(std::move(Report));
66 }
67 
68 void ento::registerPutenvStackArray(CheckerManager &Mgr) {
69   Mgr.registerChecker<PutenvStackArrayChecker>();
70 }
71 
72 bool ento::shouldRegisterPutenvStackArray(const CheckerManager &) {
73   return true;
74 }
75