Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "ParentVirtualCallCheck.h"
#include "PointerArithmeticOnPolymorphicObjectCheck.h"
#include "PosixReturnCheck.h"
#include "ProperlySeededRandomGeneratorCheck.h"
#include "RawMemoryCallOnNonTrivialTypeCheck.h"
#include "RedundantBranchConditionCheck.h"
#include "ReservedIdentifierCheck.h"
Expand Down Expand Up @@ -227,6 +228,8 @@ class BugproneModule : public ClangTidyModule {
CheckFactories.registerCheck<ParentVirtualCallCheck>(
"bugprone-parent-virtual-call");
CheckFactories.registerCheck<PosixReturnCheck>("bugprone-posix-return");
CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
"bugprone-random-generator-seed");
CheckFactories.registerCheck<RawMemoryCallOnNonTrivialTypeCheck>(
"bugprone-raw-memory-call-on-non-trivial-type");
CheckFactories.registerCheck<ReservedIdentifierCheck>(
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ add_clang_library(clangTidyBugproneModule STATIC
ParentVirtualCallCheck.cpp
PointerArithmeticOnPolymorphicObjectCheck.cpp
PosixReturnCheck.cpp
ProperlySeededRandomGeneratorCheck.cpp
RawMemoryCallOnNonTrivialTypeCheck.cpp
RedundantBranchConditionCheck.cpp
ReservedIdentifierCheck.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_PROPERLYSEEDEDRANDOMGENERATORCHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_PROPERLYSEEDEDRANDOMGENERATORCHECK_H
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_PROPERLY_SEEDED_RANDOM_GENERATOR_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_PROPERLY_SEEDED_RANDOM_GENERATOR_H

#include "../ClangTidyCheck.h"
#include <string>

namespace clang::tidy::cert {
namespace clang::tidy::bugprone {

/// Random number generator must be seeded properly.
///
/// A random number generator initialized with default value or a
/// constant expression is a security vulnerability.
///
/// For the user-facing documentation see:
/// https://clang.llvm.org/extra/clang-tidy/checks/cert/msc51-cpp.html
/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/random-generator-seed.html
class ProperlySeededRandomGeneratorCheck : public ClangTidyCheck {
public:
ProperlySeededRandomGeneratorCheck(StringRef Name, ClangTidyContext *Context);
Expand All @@ -37,6 +37,6 @@ class ProperlySeededRandomGeneratorCheck : public ClangTidyCheck {
SmallVector<StringRef, 5> DisallowedSeedTypes;
};

} // namespace clang::tidy::cert
} // namespace clang::tidy::bugprone

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_PROPERLYSEEDEDRANDOMGENERATORCHECK_H
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_PROPERLY_SEEDED_RANDOM_GENERATOR_H
6 changes: 4 additions & 2 deletions clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "../bugprone/DefaultOperatorNewOnOveralignedTypeCheck.h"
#include "../bugprone/FloatLoopCounterCheck.h"
#include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h"
#include "../bugprone/ProperlySeededRandomGeneratorCheck.h"
#include "../bugprone/RawMemoryCallOnNonTrivialTypeCheck.h"
#include "../bugprone/ReservedIdentifierCheck.h"
#include "../bugprone/SignalHandlerCheck.h"
Expand All @@ -41,6 +42,7 @@
#include "../readability/UppercaseLiteralSuffixCheck.h"
#include "LimitedRandomnessCheck.h"
#include "ProperlySeededRandomGeneratorCheck.h"
#include "MutatingCopyCheck.h"
#include "ThrownExceptionTypeCheck.h"

namespace {
Expand Down Expand Up @@ -271,7 +273,7 @@ class CERTModule : public ClangTidyModule {
"cert-mem57-cpp");
// MSC
CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc50-cpp");
CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
CheckFactories.registerCheck<bugprone::ProperlySeededRandomGeneratorCheck>(
"cert-msc51-cpp");
CheckFactories.registerCheck<bugprone::SignalHandlerCheck>(
"cert-msc54-cpp");
Expand Down Expand Up @@ -324,7 +326,7 @@ class CERTModule : public ClangTidyModule {
CheckFactories.registerCheck<bugprone::UnsafeFunctionsCheck>(
"cert-msc24-c");
CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc30-c");
CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
CheckFactories.registerCheck<bugprone::ProperlySeededRandomGeneratorCheck>(
"cert-msc32-c");
CheckFactories.registerCheck<bugprone::UnsafeFunctionsCheck>(
"cert-msc33-c");
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/cert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_clang_library(clangTidyCERTModule STATIC
CERTTidyModule.cpp
LimitedRandomnessCheck.cpp
ProperlySeededRandomGeneratorCheck.cpp
MutatingCopyCheck.cpp
ThrownExceptionTypeCheck.cpp

LINK_LIBS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.. title:: clang-tidy - bugprone-random-generator-seed

bugprone-random-generator-seed
==============================

This check flags all pseudo-random number engines, engine adaptor
instantiations and ``srand()`` when initialized or seeded with default argument,
constant expression or any user-configurable type. Pseudo-random number
engines seeded with a predictable value may cause vulnerabilities e.g. in
security protocols.
This is a CERT security rule, see
`MSC51-CPP. Ensure your random number generator is properly seeded
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/MSC51-CPP.+Ensure+your+random+number+generator+is+properly+seeded>`_ and
`MSC32-C. Properly seed pseudorandom number generators
<https://wiki.sei.cmu.edu/confluence/display/c/MSC32-C.+Properly+seed+pseudorandom+number+generators>`_.

Examples:

.. code-block:: c++

void foo() {
std::mt19937 engine1; // Diagnose, always generate the same sequence
std::mt19937 engine2(1); // Diagnose
engine1.seed(); // Diagnose
engine2.seed(1); // Diagnose

std::time_t t;
engine1.seed(std::time(&t)); // Diagnose, system time might be controlled by user

int x = atoi(argv[1]);
std::mt19937 engine3(x); // Will not warn
}

Options
-------

.. option:: DisallowedSeedTypes

A comma-separated list of the type names which are disallowed.
Default value is `time_t,std::time_t`.
2 changes: 1 addition & 1 deletion clang-tools-extra/docs/clang-tidy/checks/cert/msc32-c.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ cert-msc32-c
============

The `cert-msc32-c` check is an alias, please see
:doc:`cert-msc51-cpp <../cert/msc51-cpp>` for more information.
:doc:`bugprone-random-generator-seed <../bugprone/random-generator-seed>` for more information.
37 changes: 2 additions & 35 deletions clang-tools-extra/docs/clang-tidy/checks/cert/msc51-cpp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,5 @@
cert-msc51-cpp
==============

This check flags all pseudo-random number engines, engine adaptor
instantiations and ``srand()`` when initialized or seeded with default argument,
constant expression or any user-configurable type. Pseudo-random number
engines seeded with a predictable value may cause vulnerabilities e.g. in
security protocols.
This is a CERT security rule, see
`MSC51-CPP. Ensure your random number generator is properly seeded
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/MSC51-CPP.+Ensure+your+random+number+generator+is+properly+seeded>`_ and
`MSC32-C. Properly seed pseudorandom number generators
<https://wiki.sei.cmu.edu/confluence/display/c/MSC32-C.+Properly+seed+pseudorandom+number+generators>`_.

Examples:

.. code-block:: c++

void foo() {
std::mt19937 engine1; // Diagnose, always generate the same sequence
std::mt19937 engine2(1); // Diagnose
engine1.seed(); // Diagnose
engine2.seed(1); // Diagnose

std::time_t t;
engine1.seed(std::time(&t)); // Diagnose, system time might be controlled by user

int x = atoi(argv[1]);
std::mt19937 engine3(x); // Will not warn
}

Options
-------

.. option:: DisallowedSeedTypes

A comma-separated list of the type names which are disallowed.
Default value is `time_t,std::time_t`.
The `cert-msc51-cpp` check is an alias, please see
:doc:`bugprone-random-generator-seed <../bugprone/random-generator-seed>` for more information.
4 changes: 2 additions & 2 deletions clang-tools-extra/docs/clang-tidy/checks/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ Clang-Tidy Checks
:doc:`bugprone-parent-virtual-call <bugprone/parent-virtual-call>`, "Yes"
:doc:`bugprone-pointer-arithmetic-on-polymorphic-object <bugprone/pointer-arithmetic-on-polymorphic-object>`,
:doc:`bugprone-posix-return <bugprone/posix-return>`, "Yes"
:doc:`bugprone-random-generator-seed <bugprone/random-generator-seed>`, "Yes"
:doc:`bugprone-raw-memory-call-on-non-trivial-type <bugprone/raw-memory-call-on-non-trivial-type>`,
:doc:`bugprone-redundant-branch-condition <bugprone/redundant-branch-condition>`, "Yes"
:doc:`bugprone-reserved-identifier <bugprone/reserved-identifier>`, "Yes"
Expand Down Expand Up @@ -181,7 +182,6 @@ Clang-Tidy Checks
:doc:`cert-err33-c <cert/err33-c>`,
:doc:`cert-err60-cpp <cert/err60-cpp>`,
:doc:`cert-msc50-cpp <cert/msc50-cpp>`,
:doc:`cert-msc51-cpp <cert/msc51-cpp>`,
:doc:`cert-oop58-cpp <cert/oop58-cpp>`,
:doc:`concurrency-mt-unsafe <concurrency/mt-unsafe>`,
:doc:`concurrency-thread-canceltype-asynchronous <concurrency/thread-canceltype-asynchronous>`,
Expand Down Expand Up @@ -458,7 +458,7 @@ Check aliases
:doc:`cert-mem57-cpp <cert/mem57-cpp>`, :doc:`bugprone-default-operator-new-on-overaligned-type <bugprone/default-operator-new-on-overaligned-type>`,
:doc:`cert-msc24-c <cert/msc24-c>`, :doc:`bugprone-unsafe-functions <bugprone/unsafe-functions>`,
:doc:`cert-msc30-c <cert/msc30-c>`, :doc:`cert-msc50-cpp <cert/msc50-cpp>`,
:doc:`cert-msc32-c <cert/msc32-c>`, :doc:`cert-msc51-cpp <cert/msc51-cpp>`,
:doc:`cert-msc32-c <cert/msc32-c>`, :doc:`bugprone-random-generator-seed <bugprone/random-generator-seed>`,
:doc:`cert-msc33-c <cert/msc33-c>`, :doc:`bugprone-unsafe-functions <bugprone/unsafe-functions>`,
:doc:`cert-msc54-cpp <cert/msc54-cpp>`, :doc:`bugprone-signal-handler <bugprone/signal-handler>`,
:doc:`cert-oop11-cpp <cert/oop11-cpp>`, :doc:`performance-move-constructor-init <performance/move-constructor-init>`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// RUN: %check_clang_tidy %s cert-msc32-c %t -- -config="{CheckOptions: {cert-msc32-c.DisallowedSeedTypes: 'some_type,time_t'}}" -- -std=c99
// RUN: %check_clang_tidy %s bugprone-random-generator-seed %t -- -config="{CheckOptions: {bugprone-random-generator-seed.DisallowedSeedTypes: 'some_type,time_t'}}" -- -std=c99

void srand(int seed);
typedef int time_t;
time_t time(time_t *t);

void f(void) {
srand(1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc32-c]
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: random number generator seeded with a constant value will generate a predictable sequence of values [bugprone-random-generator-seed]

const int a = 1;
srand(a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc32-c]
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: random number generator seeded with a constant value will generate a predictable sequence of values [bugprone-random-generator-seed]

time_t t;
srand(time(&t)); // Disallowed seed type
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc32-c]
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [bugprone-random-generator-seed]
}

void g(void) {
Expand Down
Loading
Loading