aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/cert/InvalidPtrChecker.cpp
blob: b2947f590c4ec135b1f57cf8f85d1c5896c79c08 (plain) (blame)
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
//== InvalidPtrChecker.cpp ------------------------------------- -*- C++ -*--=//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines InvalidPtrChecker which finds usages of possibly
// invalidated pointer.
// CERT SEI Rules ENV31-C and ENV34-C
// For more information see:
// https://wiki.sei.cmu.edu/confluence/x/8tYxBQ
// https://wiki.sei.cmu.edu/confluence/x/5NUxBQ
//===----------------------------------------------------------------------===//

#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"

using namespace clang;
using namespace ento;

namespace {

class InvalidPtrChecker
    : public Checker<check::Location, check::BeginFunction, check::PostCall> {
private:
  // For accurate emission of NoteTags, the BugType of this checker should have
  // a unique address.
  BugType InvalidPtrBugType{this, "Use of invalidated pointer",
                            categories::MemoryError};

  void EnvpInvalidatingCall(const CallEvent &Call, CheckerContext &C) const;

  using HandlerFn = void (InvalidPtrChecker::*)(const CallEvent &Call,
                                                CheckerContext &C) const;

  // SEI CERT ENV31-C

  // If set to true, consider getenv calls as invalidating operations on the
  // environment variable buffer. This is implied in the standard, but in
  // practice does not cause problems (in the commonly used environments).
  bool InvalidatingGetEnv = false;

  // GetEnv can be treated invalidating and non-invalidating as well.
  const CallDescription GetEnvCall{{"getenv"}, 1};

  const CallDescriptionMap<HandlerFn> EnvpInvalidatingFunctions = {
      {{{"setenv"}, 3}, &InvalidPtrChecker::EnvpInvalidatingCall},
      {{{"unsetenv"}, 1}, &InvalidPtrChecker::EnvpInvalidatingCall},
      {{{"putenv"}, 1}, &InvalidPtrChecker::EnvpInvalidatingCall},
      {{{"_putenv_s"}, 2}, &InvalidPtrChecker::EnvpInvalidatingCall},
      {{{"_wputenv_s"}, 2}, &InvalidPtrChecker::EnvpInvalidatingCall},
  };

  void postPreviousReturnInvalidatingCall(const CallEvent &Call,
                                          CheckerContext &C) const;

  // SEI CERT ENV34-C
  const CallDescriptionMap<HandlerFn> PreviousCallInvalidatingFunctions = {
      {{{"setlocale"}, 2},
       &InvalidPtrChecker::postPreviousReturnInvalidatingCall},
      {{{"strerror"}, 1},
       &InvalidPtrChecker::postPreviousReturnInvalidatingCall},
      {{{"localeconv"}, 0},
       &InvalidPtrChecker::postPreviousReturnInvalidatingCall},
      {{{"asctime"}, 1},
       &InvalidPtrChecker::postPreviousReturnInvalidatingCall},
  };

  // The private members of this checker corresponding to commandline options
  // are set in this function.
  friend void ento::registerInvalidPtrChecker(CheckerManager &);

public:
  // Obtain the environment pointer from 'main()' (if present).
  void checkBeginFunction(CheckerContext &C) const;

  // Handle functions in EnvpInvalidatingFunctions, that invalidate environment
  // pointer from 'main()'
  // Handle functions in PreviousCallInvalidatingFunctions.
  // Also, check if invalidated region is passed to a
  // conservatively evaluated function call as an argument.
  void checkPostCall(const CallEvent &Call, CheckerContext &C) const;

  // Check if invalidated region is being dereferenced.
  void checkLocation(SVal l, bool isLoad, const Stmt *S,
                     CheckerContext &C) const;

private:
  const NoteTag *createEnvInvalidationNote(CheckerContext &C,
                                           ProgramStateRef State,
                                           StringRef FunctionName) const;
};

} // namespace

// Set of memory regions that were invalidated
REGISTER_SET_WITH_PROGRAMSTATE(InvalidMemoryRegions, const MemRegion *)

// Stores the region of the environment pointer of 'main' (if present).
REGISTER_TRAIT_WITH_PROGRAMSTATE(MainEnvPtrRegion, const MemRegion *)

// Stores the regions of environments returned by getenv calls.
REGISTER_SET_WITH_PROGRAMSTATE(GetenvEnvPtrRegions, const MemRegion *)

// Stores key-value pairs, where key is function declaration and value is
// pointer to memory region returned by previous call of this function
REGISTER_MAP_WITH_PROGRAMSTATE(PreviousCallResultMap, const FunctionDecl *,
                               const MemRegion *)

const NoteTag *InvalidPtrChecker::createEnvInvalidationNote(
    CheckerContext &C, ProgramStateRef State, StringRef FunctionName) const {

  const MemRegion *MainRegion = State->get<MainEnvPtrRegion>();
  const auto GetenvRegions = State->get<GetenvEnvPtrRegions>();

  return C.getNoteTag([this, MainRegion, GetenvRegions,
                       FunctionName = std::string{FunctionName}](
                          PathSensitiveBugReport &BR, llvm::raw_ostream &Out) {
    // Only handle the BugType of this checker.
    if (&BR.getBugType() != &InvalidPtrBugType)
      return;

    // Mark all regions that were interesting before as NOT interesting now
    // to avoid extra notes coming from invalidation points higher up the
    // bugpath. This ensures that only the last invalidation point is marked
    // with a note tag.
    llvm::SmallVector<std::string, 2> InvalidLocationNames;
    if (BR.isInteresting(MainRegion)) {
      BR.markNotInteresting(MainRegion);
      InvalidLocationNames.push_back("the environment parameter of 'main'");
    }
    bool InterestingGetenvFound = false;
    for (const MemRegion *MR : GetenvRegions) {
      if (BR.isInteresting(MR)) {
        BR.markNotInteresting(MR);
        if (!InterestingGetenvFound) {
          InterestingGetenvFound = true;
          InvalidLocationNames.push_back(
              "the environment returned by 'getenv'");
        }
      }
    }

    // Emit note tag message.
    if (InvalidLocationNames.size() >= 1)
      Out << '\'' << FunctionName << "' call may invalidate "
          << InvalidLocationNames[0];
    if (InvalidLocationNames.size() == 2)
      Out << ", and " << InvalidLocationNames[1];
  });
}

void InvalidPtrChecker::EnvpInvalidatingCall(const CallEvent &Call,
                                             CheckerContext &C) const {
  // This callevent invalidates all previously generated pointers to the
  // environment.
  ProgramStateRef State = C.getState();
  if (const MemRegion *MainEnvPtr = State->get<MainEnvPtrRegion>())
    State = State->add<InvalidMemoryRegions>(MainEnvPtr);
  for (const MemRegion *EnvPtr : State->get<GetenvEnvPtrRegions>())
    State = State->add<InvalidMemoryRegions>(EnvPtr);

  StringRef FunctionName = Call.getCalleeIdentifier()->getName();
  const NoteTag *InvalidationNote =
      createEnvInvalidationNote(C, State, FunctionName);

  C.addTransition(State, InvalidationNote);
}

void InvalidPtrChecker::postPreviousReturnInvalidatingCall(
    const CallEvent &Call, CheckerContext &C) const {
  ProgramStateRef State = C.getState();

  const NoteTag *Note = nullptr;
  const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
  // Invalidate the region of the previously returned pointer - if there was
  // one.
  if (const MemRegion *const *Reg = State->get<PreviousCallResultMap>(FD)) {
    const MemRegion *PrevReg = *Reg;
    State = State->add<InvalidMemoryRegions>(PrevReg);
    Note = C.getNoteTag([this, PrevReg, FD](PathSensitiveBugReport &BR,
                                            llvm::raw_ostream &Out) {
      if (!BR.isInteresting(PrevReg) || &BR.getBugType() != &InvalidPtrBugType)
        return;
      Out << '\'';
      FD->getNameForDiagnostic(Out, FD->getASTContext().getLangOpts(), true);
      Out << "' call may invalidate the result of the previous " << '\'';
      FD->getNameForDiagnostic(Out, FD->getASTContext().getLangOpts(), true);
      Out << '\'';
    });
  }

  const LocationContext *LCtx = C.getLocationContext();
  const auto *CE = cast<CallExpr>(Call.getOriginExpr());

  // Function call will return a pointer to the new symbolic region.
  DefinedOrUnknownSVal RetVal = C.getSValBuilder().conjureSymbolVal(
      CE, LCtx, CE->getType(), C.blockCount());
  State = State->BindExpr(CE, LCtx, RetVal);

  const auto *SymRegOfRetVal =
      dyn_cast_or_null<SymbolicRegion>(RetVal.getAsRegion());
  if (!SymRegOfRetVal)
    return;

  // Remember to this region.
  const MemRegion *MR = SymRegOfRetVal->getBaseRegion();
  State = State->set<PreviousCallResultMap>(FD, MR);

  ExplodedNode *Node = C.addTransition(State, Note);
  const NoteTag *PreviousCallNote = C.getNoteTag(
      [this, MR](PathSensitiveBugReport &BR, llvm::raw_ostream &Out) {
        if (!BR.isInteresting(MR) || &BR.getBugType() != &InvalidPtrBugType)
          return;
        Out << "previous function call was here";
      });

  C.addTransition(State, Node, PreviousCallNote);
}

// TODO: This seems really ugly. Simplify this.
static const MemRegion *findInvalidatedSymbolicBase(ProgramStateRef State,
                                                    const MemRegion *Reg) {
  while (Reg) {
    if (State->contains<InvalidMemoryRegions>(Reg))
      return Reg;
    const auto *SymBase = Reg->getSymbolicBase();
    if (!SymBase)
      break;
    const auto *SRV = dyn_cast<SymbolRegionValue>(SymBase->getSymbol());
    if (!SRV)
      break;
    Reg = SRV->getRegion();
    if (const auto *VarReg = dyn_cast<VarRegion>(SRV->getRegion()))
      Reg = VarReg;
  }
  return nullptr;
}

// Handle functions in EnvpInvalidatingFunctions, that invalidate environment
// pointer from 'main()' Also, check if invalidated region is passed to a
// function call as an argument.
void InvalidPtrChecker::checkPostCall(const CallEvent &Call,
                                      CheckerContext &C) const {

  ProgramStateRef State = C.getState();

  // Model 'getenv' calls
  if (GetEnvCall.matches(Call)) {
    const MemRegion *Region = Call.getReturnValue().getAsRegion();
    if (Region) {
      State = State->add<GetenvEnvPtrRegions>(Region);
      C.addTransition(State);
    }
  }

  // Check if function invalidates 'envp' argument of 'main'
  if (const auto *Handler = EnvpInvalidatingFunctions.lookup(Call))
    (this->**Handler)(Call, C);

  // Check if function invalidates the result of previous call
  if (const auto *Handler = PreviousCallInvalidatingFunctions.lookup(Call))
    (this->**Handler)(Call, C);

  // If pedantic mode is on, regard 'getenv' calls invalidating as well
  if (InvalidatingGetEnv && GetEnvCall.matches(Call))
    postPreviousReturnInvalidatingCall(Call, C);

  // Check if one of the arguments of the function call is invalidated

  // If call was inlined, don't report invalidated argument
  if (C.wasInlined)
    return;

  for (unsigned I = 0, NumArgs = Call.getNumArgs(); I < NumArgs; ++I) {

    if (const auto *SR = dyn_cast_or_null<SymbolicRegion>(
            Call.getArgSVal(I).getAsRegion())) {
      if (const MemRegion *InvalidatedSymbolicBase =
              findInvalidatedSymbolicBase(State, SR)) {
        ExplodedNode *ErrorNode = C.generateNonFatalErrorNode();
        if (!ErrorNode)
          return;

        SmallString<256> Msg;
        llvm::raw_svector_ostream Out(Msg);
        Out << "use of invalidated pointer '";
        Call.getArgExpr(I)->printPretty(Out, /*Helper=*/nullptr,
                                        C.getASTContext().getPrintingPolicy());
        Out << "' in a function call";

        auto Report = std::make_unique<PathSensitiveBugReport>(
            InvalidPtrBugType, Out.str(), ErrorNode);
        Report->markInteresting(InvalidatedSymbolicBase);
        Report->addRange(Call.getArgSourceRange(I));
        C.emitReport(std::move(Report));
      }
    }
  }
}

// Obtain the environment pointer from 'main()', if present.
void InvalidPtrChecker::checkBeginFunction(CheckerContext &C) const {
  if (!C.inTopFrame())
    return;

  const auto *FD = dyn_cast<FunctionDecl>(C.getLocationContext()->getDecl());
  if (!FD || FD->param_size() != 3 || !FD->isMain())
    return;

  ProgramStateRef State = C.getState();
  const MemRegion *EnvpReg =
      State->getRegion(FD->parameters()[2], C.getLocationContext());

  // Save the memory region pointed by the environment pointer parameter of
  // 'main'.
  C.addTransition(State->set<MainEnvPtrRegion>(EnvpReg));
}

// Check if invalidated region is being dereferenced.
void InvalidPtrChecker::checkLocation(SVal Loc, bool isLoad, const Stmt *S,
                                      CheckerContext &C) const {
  ProgramStateRef State = C.getState();

  // Ignore memory operations involving 'non-invalidated' locations.
  const MemRegion *InvalidatedSymbolicBase =
      findInvalidatedSymbolicBase(State, Loc.getAsRegion());
  if (!InvalidatedSymbolicBase)
    return;

  ExplodedNode *ErrorNode = C.generateNonFatalErrorNode();
  if (!ErrorNode)
    return;

  auto Report = std::make_unique<PathSensitiveBugReport>(
      InvalidPtrBugType, "dereferencing an invalid pointer", ErrorNode);
  Report->markInteresting(InvalidatedSymbolicBase);
  C.emitReport(std::move(Report));
}

void ento::registerInvalidPtrChecker(CheckerManager &Mgr) {
  auto *Checker = Mgr.registerChecker<InvalidPtrChecker>();
  Checker->InvalidatingGetEnv =
      Mgr.getAnalyzerOptions().getCheckerBooleanOption(Checker,
                                                       "InvalidatingGetEnv");
}

bool ento::shouldRegisterInvalidPtrChecker(const CheckerManager &) {
  return true;
}