Skip to content

Commit 315d705

Browse files
[clang-tidy] Move 'cert-msc51-cpp', 'cert-msc32-c' checks outside of 'cert' module and give a proper name (llvm#167143)
…'cert' module and give a proper name Summary: This change addresses sub-issue llvm#157296 of parent issue llvm#157287, which aims to clean up the `cert` module by moving checks that don't directly map to CERT security standards to more appropriate modules. The checks `cert-msc51-cpp` and `cert-msc32-c` both implement the same logic to detect random number generators that are not properly seeded. This is a generic bug pattern rather than a CERT-specific security constraint, making it more suitable for the `bugprone` module. Closes: llvm#157296 --------- Co-authored-by: Baranov Victor <[email protected]>
1 parent bb0bd38 commit 315d705

File tree

13 files changed

+155
-119
lines changed

13 files changed

+155
-119
lines changed

clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "ParentVirtualCallCheck.h"
6666
#include "PointerArithmeticOnPolymorphicObjectCheck.h"
6767
#include "PosixReturnCheck.h"
68+
#include "RandomGeneratorSeedCheck.h"
6869
#include "RawMemoryCallOnNonTrivialTypeCheck.h"
6970
#include "RedundantBranchConditionCheck.h"
7071
#include "ReservedIdentifierCheck.h"
@@ -230,6 +231,8 @@ class BugproneModule : public ClangTidyModule {
230231
CheckFactories.registerCheck<ParentVirtualCallCheck>(
231232
"bugprone-parent-virtual-call");
232233
CheckFactories.registerCheck<PosixReturnCheck>("bugprone-posix-return");
234+
CheckFactories.registerCheck<RandomGeneratorSeedCheck>(
235+
"bugprone-random-generator-seed");
233236
CheckFactories.registerCheck<RawMemoryCallOnNonTrivialTypeCheck>(
234237
"bugprone-raw-memory-call-on-non-trivial-type");
235238
CheckFactories.registerCheck<ReservedIdentifierCheck>(

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ add_clang_library(clangTidyBugproneModule STATIC
6666
ParentVirtualCallCheck.cpp
6767
PointerArithmeticOnPolymorphicObjectCheck.cpp
6868
PosixReturnCheck.cpp
69+
RandomGeneratorSeedCheck.cpp
6970
RawMemoryCallOnNonTrivialTypeCheck.cpp
7071
RedundantBranchConditionCheck.cpp
7172
ReservedIdentifierCheck.cpp

clang-tools-extra/clang-tidy/cert/ProperlySeededRandomGeneratorCheck.cpp renamed to clang-tools-extra/clang-tidy/bugprone/RandomGeneratorSeedCheck.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,28 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "ProperlySeededRandomGeneratorCheck.h"
9+
#include "RandomGeneratorSeedCheck.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
1212
#include "llvm/ADT/STLExtras.h"
1313

1414
using namespace clang::ast_matchers;
1515

16-
namespace clang::tidy::cert {
16+
namespace clang::tidy::bugprone {
1717

18-
ProperlySeededRandomGeneratorCheck::ProperlySeededRandomGeneratorCheck(
19-
StringRef Name, ClangTidyContext *Context)
18+
RandomGeneratorSeedCheck::RandomGeneratorSeedCheck(StringRef Name,
19+
ClangTidyContext *Context)
2020
: ClangTidyCheck(Name, Context),
2121
RawDisallowedSeedTypes(
2222
Options.get("DisallowedSeedTypes", "time_t,std::time_t")) {
2323
RawDisallowedSeedTypes.split(DisallowedSeedTypes, ',');
2424
}
2525

26-
void ProperlySeededRandomGeneratorCheck::storeOptions(
27-
ClangTidyOptions::OptionMap &Opts) {
26+
void RandomGeneratorSeedCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
2827
Options.store(Opts, "DisallowedSeedTypes", RawDisallowedSeedTypes);
2928
}
3029

31-
void ProperlySeededRandomGeneratorCheck::registerMatchers(MatchFinder *Finder) {
30+
void RandomGeneratorSeedCheck::registerMatchers(MatchFinder *Finder) {
3231
auto RandomGeneratorEngineDecl = cxxRecordDecl(hasAnyName(
3332
"::std::linear_congruential_engine", "::std::mersenne_twister_engine",
3433
"::std::subtract_with_carry_engine", "::std::discard_block_engine",
@@ -75,8 +74,7 @@ void ProperlySeededRandomGeneratorCheck::registerMatchers(MatchFinder *Finder) {
7574
this);
7675
}
7776

78-
void ProperlySeededRandomGeneratorCheck::check(
79-
const MatchFinder::MatchResult &Result) {
77+
void RandomGeneratorSeedCheck::check(const MatchFinder::MatchResult &Result) {
8078
const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructExpr>("ctor");
8179
if (Ctor)
8280
checkSeed(Result, Ctor);
@@ -91,8 +89,8 @@ void ProperlySeededRandomGeneratorCheck::check(
9189
}
9290

9391
template <class T>
94-
void ProperlySeededRandomGeneratorCheck::checkSeed(
95-
const MatchFinder::MatchResult &Result, const T *Func) {
92+
void RandomGeneratorSeedCheck::checkSeed(const MatchFinder::MatchResult &Result,
93+
const T *Func) {
9694
if (Func->getNumArgs() == 0 || Func->getArg(0)->isDefaultArgument()) {
9795
diag(Func->getExprLoc(),
9896
"random number generator seeded with a default argument will generate "
@@ -118,4 +116,4 @@ void ProperlySeededRandomGeneratorCheck::checkSeed(
118116
}
119117
}
120118

121-
} // namespace clang::tidy::cert
119+
} // namespace clang::tidy::bugprone

clang-tools-extra/clang-tidy/cert/ProperlySeededRandomGeneratorCheck.h renamed to clang-tools-extra/clang-tidy/bugprone/RandomGeneratorSeedCheck.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_PROPERLYSEEDEDRANDOMGENERATORCHECK_H
10-
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_PROPERLYSEEDEDRANDOMGENERATORCHECK_H
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RANDOMGENERATORSEEDCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RANDOMGENERATORSEEDCHECK_H
1111

1212
#include "../ClangTidyCheck.h"
1313
#include <string>
1414

15-
namespace clang::tidy::cert {
15+
namespace clang::tidy::bugprone {
1616

1717
/// Random number generator must be seeded properly.
1818
///
1919
/// A random number generator initialized with default value or a
2020
/// constant expression is a security vulnerability.
2121
///
2222
/// For the user-facing documentation see:
23-
/// https://clang.llvm.org/extra/clang-tidy/checks/cert/msc51-cpp.html
24-
class ProperlySeededRandomGeneratorCheck : public ClangTidyCheck {
23+
/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/random-generator-seed.html
24+
class RandomGeneratorSeedCheck : public ClangTidyCheck {
2525
public:
26-
ProperlySeededRandomGeneratorCheck(StringRef Name, ClangTidyContext *Context);
26+
RandomGeneratorSeedCheck(StringRef Name, ClangTidyContext *Context);
2727
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
2828
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2929
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
@@ -37,6 +37,6 @@ class ProperlySeededRandomGeneratorCheck : public ClangTidyCheck {
3737
SmallVector<StringRef, 5> DisallowedSeedTypes;
3838
};
3939

40-
} // namespace clang::tidy::cert
40+
} // namespace clang::tidy::bugprone
4141

42-
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_PROPERLYSEEDEDRANDOMGENERATORCHECK_H
42+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RANDOMGENERATORSEEDCHECK_H

clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "../bugprone/ExceptionCopyConstructorThrowsCheck.h"
1717
#include "../bugprone/FloatLoopCounterCheck.h"
1818
#include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h"
19+
#include "../bugprone/RandomGeneratorSeedCheck.h"
1920
#include "../bugprone/RawMemoryCallOnNonTrivialTypeCheck.h"
2021
#include "../bugprone/ReservedIdentifierCheck.h"
2122
#include "../bugprone/SignalHandlerCheck.h"
@@ -41,7 +42,6 @@
4142
#include "../readability/EnumInitialValueCheck.h"
4243
#include "../readability/UppercaseLiteralSuffixCheck.h"
4344
#include "LimitedRandomnessCheck.h"
44-
#include "ProperlySeededRandomGeneratorCheck.h"
4545

4646
namespace {
4747

@@ -272,7 +272,7 @@ class CERTModule : public ClangTidyModule {
272272
"cert-mem57-cpp");
273273
// MSC
274274
CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc50-cpp");
275-
CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
275+
CheckFactories.registerCheck<bugprone::RandomGeneratorSeedCheck>(
276276
"cert-msc51-cpp");
277277
CheckFactories.registerCheck<bugprone::SignalHandlerCheck>(
278278
"cert-msc54-cpp");
@@ -325,7 +325,7 @@ class CERTModule : public ClangTidyModule {
325325
CheckFactories.registerCheck<bugprone::UnsafeFunctionsCheck>(
326326
"cert-msc24-c");
327327
CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc30-c");
328-
CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
328+
CheckFactories.registerCheck<bugprone::RandomGeneratorSeedCheck>(
329329
"cert-msc32-c");
330330
CheckFactories.registerCheck<bugprone::UnsafeFunctionsCheck>(
331331
"cert-msc33-c");

clang-tools-extra/clang-tidy/cert/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ set(LLVM_LINK_COMPONENTS
66
add_clang_library(clangTidyCERTModule STATIC
77
CERTTidyModule.cpp
88
LimitedRandomnessCheck.cpp
9-
ProperlySeededRandomGeneratorCheck.cpp
109

1110
LINK_LIBS
1211
clangTidy

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ New check aliases
283283
<clang-tidy/checks/bugprone/default-operator-new-on-overaligned-type>`
284284
keeping initial check as an alias to the new one.
285285

286+
- Renamed :doc:`cert-msc32-c <clang-tidy/checks/cert/msc32-c>` to
287+
:doc:`bugprone-random-generator-seed
288+
<clang-tidy/checks/bugprone/random-generator-seed>`
289+
keeping initial check as an alias to the new one.
290+
291+
- Renamed :doc:`cert-msc51-cpp <clang-tidy/checks/cert/msc51-cpp>` to
292+
:doc:`bugprone-random-generator-seed
293+
<clang-tidy/checks/bugprone/random-generator-seed>`
294+
keeping initial check as an alias to the new one.
295+
286296
- Renamed :doc:`cert-oop57-cpp <clang-tidy/checks/cert/oop57-cpp>` to
287297
:doc:`bugprone-raw-memory-call-on-non-trivial-type
288298
<clang-tidy/checks/bugprone/raw-memory-call-on-non-trivial-type>`
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
.. title:: clang-tidy - bugprone-random-generator-seed
2+
3+
bugprone-random-generator-seed
4+
==============================
5+
6+
Flags all pseudo-random number engines, engine adaptor
7+
instantiations and ``srand()`` when initialized or seeded with default argument,
8+
constant expression or any user-configurable type. Pseudo-random number
9+
engines seeded with a predictable value may cause vulnerabilities e.g. in
10+
security protocols.
11+
12+
Examples:
13+
14+
.. code-block:: c++
15+
16+
void foo() {
17+
std::mt19937 engine1; // Diagnose, always generate the same sequence
18+
std::mt19937 engine2(1); // Diagnose
19+
engine1.seed(); // Diagnose
20+
engine2.seed(1); // Diagnose
21+
22+
std::time_t t;
23+
engine1.seed(std::time(&t)); // Diagnose, system time might be controlled by user
24+
25+
int x = atoi(argv[1]);
26+
std::mt19937 engine3(x); // Will not warn
27+
}
28+
29+
Options
30+
-------
31+
32+
.. option:: DisallowedSeedTypes
33+
34+
A comma-separated list of the type names which are disallowed.
35+
Default value is `time_t,std::time_t`.
36+
37+
References
38+
----------
39+
40+
This check corresponds to the CERT C++ Coding Standard rules
41+
`MSC51-CPP. Ensure your random number generator is properly seeded
42+
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/MSC51-CPP.+Ensure+your+random+number+generator+is+properly+seeded>`_ and
43+
`MSC32-C. Properly seed pseudorandom number generators
44+
<https://wiki.sei.cmu.edu/confluence/display/c/MSC32-C.+Properly+seed+pseudorandom+number+generators>`_.
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
.. title:: clang-tidy - cert-msc32-c
22
.. meta::
3-
:http-equiv=refresh: 5;URL=../cert/msc51-cpp.html
3+
:http-equiv=refresh: 5;URL=../bugprone/random-generator-seed.html
44

55
cert-msc32-c
66
============
77

88
The `cert-msc32-c` check is an alias, please see
9-
:doc:`cert-msc51-cpp <../cert/msc51-cpp>` for more information.
9+
:doc:`bugprone-random-generator-seed <../bugprone/random-generator-seed>`
10+
for more information.
11+
12+
This check corresponds to the CERT C Coding Standard rule
13+
`MSC32-C. Properly seed pseudorandom number generators
14+
<https://wiki.sei.cmu.edu/confluence/display/c/MSC32-C.+Properly+seed+pseudorandom+number+generators>`_.
Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,14 @@
11
.. title:: clang-tidy - cert-msc51-cpp
2+
.. meta::
3+
:http-equiv=refresh: 5;URL=../bugprone/random-generator-seed.html
24

35
cert-msc51-cpp
46
==============
57

6-
This check flags all pseudo-random number engines, engine adaptor
7-
instantiations and ``srand()`` when initialized or seeded with default argument,
8-
constant expression or any user-configurable type. Pseudo-random number
9-
engines seeded with a predictable value may cause vulnerabilities e.g. in
10-
security protocols.
11-
This is a CERT security rule, see
12-
`MSC51-CPP. Ensure your random number generator is properly seeded
13-
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/MSC51-CPP.+Ensure+your+random+number+generator+is+properly+seeded>`_ and
14-
`MSC32-C. Properly seed pseudorandom number generators
15-
<https://wiki.sei.cmu.edu/confluence/display/c/MSC32-C.+Properly+seed+pseudorandom+number+generators>`_.
16-
17-
Examples:
18-
19-
.. code-block:: c++
20-
21-
void foo() {
22-
std::mt19937 engine1; // Diagnose, always generate the same sequence
23-
std::mt19937 engine2(1); // Diagnose
24-
engine1.seed(); // Diagnose
25-
engine2.seed(1); // Diagnose
26-
27-
std::time_t t;
28-
engine1.seed(std::time(&t)); // Diagnose, system time might be controlled by user
8+
The `cert-msc51-cpp` check is an alias, please see
9+
:doc:`bugprone-random-generator-seed <../bugprone/random-generator-seed>`
10+
for more information.
2911

30-
int x = atoi(argv[1]);
31-
std::mt19937 engine3(x); // Will not warn
32-
}
33-
34-
Options
35-
-------
36-
37-
.. option:: DisallowedSeedTypes
38-
39-
A comma-separated list of the type names which are disallowed.
40-
Default value is `time_t,std::time_t`.
12+
This check corresponds to the CERT C++ Coding Standard rule
13+
`MSC51-CPP. Ensure your random number generator is properly seeded
14+
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/MSC51-CPP.+Ensure+your+random+number+generator+is+properly+seeded>`_.

0 commit comments

Comments
 (0)