KEMBAR78
C++ Secure Programming | ODP
#include <mutex>
#include <thread>
const size_t maxThreads = 10;
void do_work(size_t i, std::mutex *pm) {
std::lock_guard<std::mutex> lk(*pm);
// Access data protected by the lock.
}
void start_threads() {
std::thread threads[maxThreads];
std::mutex m;
for (size_t i = 0; i < maxThreads; ++i) {
threads[i] = std::thread(do_work, i, &m);
}
}
Writing Secure C++
427 pages
117 pages
● Index out of bounds
● Pointer Arithmetic
● Invalid Pointer, References & Iterators
● Uninitialized Variables
● Memory leaks
● Dereferencing NULL Pointers
● Copy Constructor & Assignment operators
● First obvious thing...
– DO NOT MIX C and C++ code
● First obvious example - MySQL
● Garbage collection
– Managed C++
● http://managedcpp.sourceforge.net
● uses precise (depends on the type safety properties)
– BoehmGC
● http://www.hboehm.info/gc/
● uses conservative (slower)
– gcnew/gcdelete methods
Index out of bounds
#define N 10
T static_array[N];
int n = 20;
T* dynamic_array= new T[n];
std::vector<T> vector_array;
array[index] = x;
Safe C++ suggests:
1. overload the [] operator
2. use vectors
Index out of bounds
std::vector<T> vector_array;
Problem with vectors
- they are dynamically allocated
- they are allocating larger chunks during
initialization
- every time the limit of a vector is reached
a new one is created and old values are copied
Index out of bounds
* Do not use static or dynamically allocated arrays.
Use a template array or vector instead.
* Do not use new and delete operators with brackets,
leave it up to the template vector to allocate
multiple elements.
* Use scpp:vector instead of std::vector and
scpp::array consistently instead of a static array,
and switch the sanity checks on.
* For a multidimensional array, use scpp::matrix and
access elements through the () operator to provide
index-out-of-bounds checks.
Pointer arithmetic
vector<int> v;
for (int i=0; i<10; i++)
v.push_back(i);
int *ptr = &v[3];
cout << “Element: “ << (*ptr) << endl;
cout << “Address: “ << ptr << endl;
cout << “Adding more elements” << endl;
for (int i=0; i<10; i++)
v.push_back(i*10);
cout << “Element: “ << (*ptr) << endl;
cout << “Address: “ << &v[3] << endl;
Element: 3
Address: 0x1001000cc
Adding more elements
Element: 3
Address: 0x10010028c
Pointer arithmetic
vector<int> v;
for (int i=0; i<10; i++)
v.push_back(i);
int* ptr = &v[3];
same as
int& ptr = v[3];
vector<int>::const_iterator new1 = v.begin;
- this has the same issue as the previous
example
The same is true for almost all STL and STL like
containers.
hash_set & hash_map are also the same
Pointer arithmetic
* Do not hold pointers, references, or iterators to
the element of a container after you’ve modified the
container.
* Avoid pointer arithmetic(where possible). Use
template vector or array with index instead.
Uninitialized Variables
* Do not use built-in types such as int, unsigned,
double, bool, etc., for class data members.
- Instead, use Int, Unsigned, Double, Bool, etc.,
because you will not need to initialize them in
constructors.
* Use these new classes instead of built-in types
for passing parameters to functions, to get
additional type safety.
Uninitialized Variables
* auto_ptr / unique_ptr
- limited garbage collection by automatically
calling delete in the destructor
* also called smart pointers
* Scoped Pointers
- can't be copied
-
Memory Leaks
{
MyClass* my_class_object = new MyClass;
DoSomething(my_class_object);
} // memory leak!!!
MyClass* my_class_object = new MyClass;
DoSomething(my_class_object);
my_class_object = NULL; // memory leak!!!
Memory Leaks
T* dynamic_array = new T[n];
delete T;
Memory Leaks
T* dynamic_array = new T[n];
delete T;
but had to be:
delete [] T;
Dereferencing NULL pointers
* If you have a pointer that owns the object it
points to, use a smart pointer (a reference counting
pointer or scoped pointer).
* When you have a raw pointer T* pointing to an
object you do not own, use the template class Ptr<T>
instead.
* For a const pointer (i.e., const T*) use
Ptr<const T>.
Copy Constructors and
Assignment Operators
Each time you add a new data member in a class
do not forget to add corresponding code to the
copy-constructor and assignment operators!
If you DON'T... C++ creates “defaults” for you.
Copy Constructors and
Assignment Operators
* Rely on default versions created for you
automatically by a compiler.
* Prohibit copies of any kind by declaring the copy
constructor and assignment operator as private, and
do not provide an implementation.
* Write your own versions.
Copy Constructors and
Assignment Operators
* Whenever possible, avoid writing a copy-constructor
or assignment operator for your classes.
* If the default versions do not work for you,
consider prohibiting the copying of instances of your
class by declaring the copy-constructor and assignment
operator private.
Others
* DCL50-CPP. Do not define a C-style variadic
function
* EXP50-CPP. Do not depend on the order of evaluation
for side effects
* MEM51-CPP. Properly deallocate dynamically
allocated resources
total 2070 pages

C++ Secure Programming

  • 1.
    #include <mutex> #include <thread> constsize_t maxThreads = 10; void do_work(size_t i, std::mutex *pm) { std::lock_guard<std::mutex> lk(*pm); // Access data protected by the lock. } void start_threads() { std::thread threads[maxThreads]; std::mutex m; for (size_t i = 0; i < maxThreads; ++i) { threads[i] = std::thread(do_work, i, &m); } } Writing Secure C++
  • 2.
  • 3.
  • 4.
    ● Index outof bounds ● Pointer Arithmetic ● Invalid Pointer, References & Iterators ● Uninitialized Variables ● Memory leaks ● Dereferencing NULL Pointers ● Copy Constructor & Assignment operators
  • 5.
    ● First obviousthing... – DO NOT MIX C and C++ code ● First obvious example - MySQL ● Garbage collection – Managed C++ ● http://managedcpp.sourceforge.net ● uses precise (depends on the type safety properties) – BoehmGC ● http://www.hboehm.info/gc/ ● uses conservative (slower) – gcnew/gcdelete methods
  • 6.
    Index out ofbounds #define N 10 T static_array[N]; int n = 20; T* dynamic_array= new T[n]; std::vector<T> vector_array; array[index] = x; Safe C++ suggests: 1. overload the [] operator 2. use vectors
  • 7.
    Index out ofbounds std::vector<T> vector_array; Problem with vectors - they are dynamically allocated - they are allocating larger chunks during initialization - every time the limit of a vector is reached a new one is created and old values are copied
  • 8.
    Index out ofbounds * Do not use static or dynamically allocated arrays. Use a template array or vector instead. * Do not use new and delete operators with brackets, leave it up to the template vector to allocate multiple elements. * Use scpp:vector instead of std::vector and scpp::array consistently instead of a static array, and switch the sanity checks on. * For a multidimensional array, use scpp::matrix and access elements through the () operator to provide index-out-of-bounds checks.
  • 9.
    Pointer arithmetic vector<int> v; for(int i=0; i<10; i++) v.push_back(i); int *ptr = &v[3]; cout << “Element: “ << (*ptr) << endl; cout << “Address: “ << ptr << endl; cout << “Adding more elements” << endl; for (int i=0; i<10; i++) v.push_back(i*10); cout << “Element: “ << (*ptr) << endl; cout << “Address: “ << &v[3] << endl; Element: 3 Address: 0x1001000cc Adding more elements Element: 3 Address: 0x10010028c
  • 10.
    Pointer arithmetic vector<int> v; for(int i=0; i<10; i++) v.push_back(i); int* ptr = &v[3]; same as int& ptr = v[3]; vector<int>::const_iterator new1 = v.begin; - this has the same issue as the previous example The same is true for almost all STL and STL like containers. hash_set & hash_map are also the same
  • 11.
    Pointer arithmetic * Donot hold pointers, references, or iterators to the element of a container after you’ve modified the container. * Avoid pointer arithmetic(where possible). Use template vector or array with index instead.
  • 12.
    Uninitialized Variables * Donot use built-in types such as int, unsigned, double, bool, etc., for class data members. - Instead, use Int, Unsigned, Double, Bool, etc., because you will not need to initialize them in constructors. * Use these new classes instead of built-in types for passing parameters to functions, to get additional type safety.
  • 13.
    Uninitialized Variables * auto_ptr/ unique_ptr - limited garbage collection by automatically calling delete in the destructor * also called smart pointers * Scoped Pointers - can't be copied -
  • 14.
    Memory Leaks { MyClass* my_class_object= new MyClass; DoSomething(my_class_object); } // memory leak!!! MyClass* my_class_object = new MyClass; DoSomething(my_class_object); my_class_object = NULL; // memory leak!!!
  • 15.
    Memory Leaks T* dynamic_array= new T[n]; delete T;
  • 16.
    Memory Leaks T* dynamic_array= new T[n]; delete T; but had to be: delete [] T;
  • 17.
    Dereferencing NULL pointers *If you have a pointer that owns the object it points to, use a smart pointer (a reference counting pointer or scoped pointer). * When you have a raw pointer T* pointing to an object you do not own, use the template class Ptr<T> instead. * For a const pointer (i.e., const T*) use Ptr<const T>.
  • 18.
    Copy Constructors and AssignmentOperators Each time you add a new data member in a class do not forget to add corresponding code to the copy-constructor and assignment operators! If you DON'T... C++ creates “defaults” for you.
  • 19.
    Copy Constructors and AssignmentOperators * Rely on default versions created for you automatically by a compiler. * Prohibit copies of any kind by declaring the copy constructor and assignment operator as private, and do not provide an implementation. * Write your own versions.
  • 20.
    Copy Constructors and AssignmentOperators * Whenever possible, avoid writing a copy-constructor or assignment operator for your classes. * If the default versions do not work for you, consider prohibiting the copying of instances of your class by declaring the copy-constructor and assignment operator private.
  • 21.
    Others * DCL50-CPP. Donot define a C-style variadic function * EXP50-CPP. Do not depend on the order of evaluation for side effects * MEM51-CPP. Properly deallocate dynamically allocated resources
  • 22.