diff --git a/C_secure_coding/SecC++.zip b/C_secure_coding/SecC++.zip new file mode 100644 index 0000000..d26e59e Binary files /dev/null and b/C_secure_coding/SecC++.zip differ diff --git a/C_secure_coding/SecC++/examples/adresses.c b/C_secure_coding/SecC++/examples/adresses.c new file mode 100644 index 0000000..769c308 --- /dev/null +++ b/C_secure_coding/SecC++/examples/adresses.c @@ -0,0 +1,18 @@ +#include +#include + +void foo(void) { +} + +int a = 12; + +int main(void) { + int b = 0; + char * a; + char * c; + strcpy(a, c); + + printf("%p, %p, %p\n", &b, &a, foo); + + return 0; +} diff --git a/C_secure_coding/SecC++/examples/data_race.cpp b/C_secure_coding/SecC++/examples/data_race.cpp new file mode 100644 index 0000000..25aed7b --- /dev/null +++ b/C_secure_coding/SecC++/examples/data_race.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +int counter = 65535; +std::mutex mtx = {}; + +void worker(int delta) { + for (auto i = 0U; i < 100; ++i) { + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + // Zła praktyka! Zawsze rób to RAII! + // mtx.lock(); + { // Critical section + std::lock_guard _(mtx); + counter += delta; + } + // mtx.unlock(); + } +} + +int main() { + auto t1 = std::thread(worker, 1); + auto t2 = std::thread(worker, -1); + + t2.join(); + t1.join(); + assert(counter == 65535); +} diff --git a/C_secure_coding/SecC++/examples/data_race_atomic.cpp b/C_secure_coding/SecC++/examples/data_race_atomic.cpp new file mode 100644 index 0000000..395e0e6 --- /dev/null +++ b/C_secure_coding/SecC++/examples/data_race_atomic.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include + +std::atomic counter = 65535; + +void worker(int delta) { + for (auto i = 0U; i < 100; ++i) { + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + counter.fetch_add(delta, std::memory_order_release); + } +} + +int main() { + std::cout << "int is atomic? " << std::atomic{}.is_lock_free() << '\n'; + + auto t1 = std::thread(worker, 1); + auto t2 = std::thread(worker, -1); + + t2.join(); + t1.join(); + assert(counter == 65535); +} diff --git a/C_secure_coding/SecC++/examples/linking/function.c b/C_secure_coding/SecC++/examples/linking/function.c new file mode 100644 index 0000000..94eb5f5 --- /dev/null +++ b/C_secure_coding/SecC++/examples/linking/function.c @@ -0,0 +1,10 @@ +#include "function.h" +#include + +void foo(void) { + printf("Hello. I'm foo().\n"); +} + +void bar(void) { + printf("Hello. I'm bar().\n"); +} diff --git a/C_secure_coding/SecC++/examples/linking/function.h b/C_secure_coding/SecC++/examples/linking/function.h new file mode 100644 index 0000000..a7ad875 --- /dev/null +++ b/C_secure_coding/SecC++/examples/linking/function.h @@ -0,0 +1,4 @@ +#pragma once + +void foo(void); +void bar(void); diff --git a/C_secure_coding/SecC++/examples/linking/main.c b/C_secure_coding/SecC++/examples/linking/main.c new file mode 100644 index 0000000..19355da --- /dev/null +++ b/C_secure_coding/SecC++/examples/linking/main.c @@ -0,0 +1,7 @@ +#include "function.h" + +int main(void) { + //foo(); + bar(); + return 0; +} diff --git a/C_secure_coding/SecC++/examples/main.c b/C_secure_coding/SecC++/examples/main.c new file mode 100644 index 0000000..fbb4a91 --- /dev/null +++ b/C_secure_coding/SecC++/examples/main.c @@ -0,0 +1,9 @@ +#include + +int main(void) { + int * data = malloc(10000); + (void) data; + + return 0; +} + diff --git a/C_secure_coding/SecC++/examples/mario.c b/C_secure_coding/SecC++/examples/mario.c new file mode 100644 index 0000000..f8a2cfc --- /dev/null +++ b/C_secure_coding/SecC++/examples/mario.c @@ -0,0 +1,16 @@ +#include + +int main(void) { + int x = 0; + printf("%d, %d\n", x, ++x); + float b = 123.4f; + (void) b; + + float a = 0.1; + + if (a > 0.1) { + printf("Ups!\n"); + } + + return 0; +} diff --git a/C_secure_coding/SecC++/examples/move_sem.cpp b/C_secure_coding/SecC++/examples/move_sem.cpp new file mode 100644 index 0000000..ef13240 --- /dev/null +++ b/C_secure_coding/SecC++/examples/move_sem.cpp @@ -0,0 +1,61 @@ +#include +#include + +class X { +public: + explicit X(const std::string& name) + : name_ptr(new std::string(name)) { + std::cout << "Construct X name= " << *name_ptr << '\n'; + } + + // non copyable + X(const X& x) = delete; + X& operator=(const X& x) = delete; + + void info() const { + std::cout << "info() in X\n"; + } + + X(X&& x) { + std::cout << "Move construct name = " << *(x.name_ptr) << '\n'; + name_ptr = x.name_ptr; + x.name_ptr = nullptr; + } + X& operator=(X&& x) { + std::cout << "Move operator= name = " << *(x.name_ptr) << '\n'; + if (name_ptr != nullptr) { + delete name_ptr; + } + name_ptr = x.name_ptr; + x.name_ptr = nullptr; + return *this; + } + ~X() { + std::cout << "Destruct name= "; + if (name_ptr != nullptr) { + std::cout << *name_ptr; + } else { + std::cout << "nullptr"; + } + std::cout << '\n'; + // Nadmiarowe i edukacyjne + if (name_ptr != nullptr) { + delete name_ptr; + } + } +private: + std::string * name_ptr; +}; + +void foo(X&& my_x) { + std::cout << "In foo()\n"; + my_x.info(); +} + +int main() { + auto x1 = X("DNA Myszy"); + auto x2 = std::move(x1); + + foo(std::move(x2)); + std::cout << "After foo()\n"; +} diff --git a/C_secure_coding/SecC++/examples/move_sem_improv.cpp b/C_secure_coding/SecC++/examples/move_sem_improv.cpp new file mode 100644 index 0000000..151c5b6 --- /dev/null +++ b/C_secure_coding/SecC++/examples/move_sem_improv.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +class X { +public: + explicit X(const std::string& name) + : name_ptr(std::make_unique(name)) { + std::cout << "Construct X name= " << *name_ptr << '\n'; + } + + // non copyable + X(const X& x) = delete; + X& operator=(const X& x) = delete; + + void info() const { + std::cout << "info() in X\n"; + } + + X(X&& x) { + std::cout << "Move construct name = " << *(x.name_ptr) << '\n'; + name_ptr = std::move(x.name_ptr); + } + X& operator=(X&& x) { + std::cout << "Move operator= name = " << *(x.name_ptr) << '\n'; + name_ptr = std::move(x.name_ptr); + return *this; + } + ~X() { + std::cout << "Destruct name= "; + if (name_ptr != nullptr) { + std::cout << *name_ptr; + } else { + std::cout << "nullptr"; + } + std::cout << '\n'; + } +private: + std::unique_ptr name_ptr; +}; + +void foo(X&& my_x) { + std::cout << "In foo()\n"; + my_x.info(); +} + +int main() { + auto x1 = X("DNA Myszy"); + auto x2 = std::move(x1); + + foo(std::move(x2)); + std::cout << "After foo()\n"; +} diff --git a/C_secure_coding/SecC++/examples/myrand.c b/C_secure_coding/SecC++/examples/myrand.c new file mode 100644 index 0000000..54eba79 --- /dev/null +++ b/C_secure_coding/SecC++/examples/myrand.c @@ -0,0 +1,3 @@ +int rand(void) { + return 42; +} diff --git a/C_secure_coding/SecC++/examples/option.cpp b/C_secure_coding/SecC++/examples/option.cpp new file mode 100644 index 0000000..2d0a770 --- /dev/null +++ b/C_secure_coding/SecC++/examples/option.cpp @@ -0,0 +1,24 @@ +#include +#include + +std::optional my_div(int a, int b) { + if (b == 0) { + return {}; + } + //return std::make_optional(a / b); + return {a / b}; +} + +int main() { + auto opt = my_div(16, 0); + //if (opt.has_value()) { + if (opt) { + std::cout << "Correct!\n"; + //std::cout << opt.value() << '\n'; + std::cout << *opt << '\n'; + } else { + std::cout << "Incorrect!\n"; + // To jest terroryzm! + std::cout << *opt << '\n'; + } +} diff --git a/C_secure_coding/SecC++/examples/prog1.c b/C_secure_coding/SecC++/examples/prog1.c new file mode 100644 index 0000000..a5399ed --- /dev/null +++ b/C_secure_coding/SecC++/examples/prog1.c @@ -0,0 +1,13 @@ +#include +#include +#include + +int main(void) { + // To jest niebezpieczne! + srand(time(0)); + + int a = rand(); + printf("%d\n", a); + + return 0; +} diff --git a/C_secure_coding/SecC++/examples/shr_ptr.cpp b/C_secure_coding/SecC++/examples/shr_ptr.cpp new file mode 100644 index 0000000..97f739a --- /dev/null +++ b/C_secure_coding/SecC++/examples/shr_ptr.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + + +class Airplane { +public: + explicit Airplane(const std::string & name_) + : name{name_} { + std::cout << "Construct Airplane name = " << name << '\n'; + } + const std::string & get_name() const { + return name; + } + ~Airplane() { + std::cout << "Destruct Airplane name = " << name << '\n'; + } +private: + const std::string name; +}; + +// std::unique_ptr process_airplane(std::unique_ptr airplane); +// +// u_p_airplane = process_airpalne(std::move(u_p_airplane)); + + +int main() { + std::shared_ptr a1; + auto a2 = std::make_shared("DC8"); + + a1 = a2; + + std::cout << a1->get_name() << '\n'; + std::cout << a2->get_name() << '\n'; + + std::cout << "Count a1: " << a1.use_count() << " a2: " << a2.use_count() << '\n'; + + std::shared_ptr a3; + a3 = std::move(a1); + + std::cout << "Count a1: " << a1.use_count() + << " a2: " << a2.use_count() << " a3: " << a3.use_count() << '\n'; + + auto a4 = std::make_unique("Mig21"); + + std::shared_ptr a5; + a5 = std::move(a4); + + u_p = std::move(s_p); +} + diff --git a/C_secure_coding/SecC++/examples/struct.c b/C_secure_coding/SecC++/examples/struct.c new file mode 100644 index 0000000..a924eb3 --- /dev/null +++ b/C_secure_coding/SecC++/examples/struct.c @@ -0,0 +1,18 @@ +#include +struct X { + int data; +}; + +struct X process(void) { + struct X x; + x.data = 42; + return x; +} + +int main(void) { + struct X x = process(); + printf("%d\n", x.data); + + return 0; + +} diff --git a/C_secure_coding/SecC++/examples/taint.cpp b/C_secure_coding/SecC++/examples/taint.cpp new file mode 100644 index 0000000..1f6ffff --- /dev/null +++ b/C_secure_coding/SecC++/examples/taint.cpp @@ -0,0 +1,16 @@ + +enum class DataType { + Taint, + NotTaint +}; + +template +struct Data { + int payload[200]; +}; + +Data foo(Data data); + +void foo(Data data); + + diff --git a/C_secure_coding/SecC++/examples/unuq_ptr.cpp b/C_secure_coding/SecC++/examples/unuq_ptr.cpp new file mode 100644 index 0000000..d06893b --- /dev/null +++ b/C_secure_coding/SecC++/examples/unuq_ptr.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + + +class Airplane { +public: + explicit Airplane(const std::string & name_) + : name{name_} { + std::cout << "Construct Airplane name = " << name << '\n'; + } + const std::string & get_name() const { + return name; + } + ~Airplane() { + std::cout << "Destruct Airplane name = " << name << '\n'; + } +private: + const std::string name; +}; + +void info(std::unique_ptr ptr) { + std::cout << "In info()\n"; + std::cout << ptr->get_name() << '\n'; + std::cout << "End of info()\n"; +} + +int main() { + std::unique_ptr a1; + { + auto a2 = std::make_unique("F22"); + + a1 = std::move(a2); + + std::cout << "End of scope\n"; + } + std::cout << "Out of scope\n"; + + std::cout << a1->get_name() << '\n'; + + auto a3 = std::make_unique("Pirat"); + + std::cout << "Before info()\n"; + info(std::move(a3)); + + std::cout << "After info()\n"; + + std::cout << "End main()\n"; +} + diff --git a/C_secure_coding/SecC++/notes/ptr_container.png b/C_secure_coding/SecC++/notes/ptr_container.png new file mode 100644 index 0000000..d9ce36b Binary files /dev/null and b/C_secure_coding/SecC++/notes/ptr_container.png differ diff --git a/C_secure_coding/SecC++/notes/ptr_struct.png b/C_secure_coding/SecC++/notes/ptr_struct.png new file mode 100644 index 0000000..35655f1 Binary files /dev/null and b/C_secure_coding/SecC++/notes/ptr_struct.png differ diff --git a/C_secure_coding/SecC++/notes/pure.png b/C_secure_coding/SecC++/notes/pure.png new file mode 100644 index 0000000..11e84d1 Binary files /dev/null and b/C_secure_coding/SecC++/notes/pure.png differ diff --git a/C_secure_coding/SecC++/notes/sec_rings.png b/C_secure_coding/SecC++/notes/sec_rings.png new file mode 100644 index 0000000..5af5f5b Binary files /dev/null and b/C_secure_coding/SecC++/notes/sec_rings.png differ diff --git a/C_secure_coding/SecC++/notes/sec_surface.png b/C_secure_coding/SecC++/notes/sec_surface.png new file mode 100644 index 0000000..8def682 Binary files /dev/null and b/C_secure_coding/SecC++/notes/sec_surface.png differ diff --git a/C_secure_coding/SecC++/slides/C90_C99_Przegląd_mechanizmów.pdf b/C_secure_coding/SecC++/slides/C90_C99_Przegląd_mechanizmów.pdf new file mode 100644 index 0000000..159dc57 Binary files /dev/null and b/C_secure_coding/SecC++/slides/C90_C99_Przegląd_mechanizmów.pdf differ diff --git a/C_secure_coding/SecC++/slides/MITRE.pdf b/C_secure_coding/SecC++/slides/MITRE.pdf new file mode 100644 index 0000000..8d3bf1a Binary files /dev/null and b/C_secure_coding/SecC++/slides/MITRE.pdf differ diff --git a/C_secure_coding/SecC++/slides/Modele_Bezpieczeństwa.pdf b/C_secure_coding/SecC++/slides/Modele_Bezpieczeństwa.pdf new file mode 100644 index 0000000..1259879 Binary files /dev/null and b/C_secure_coding/SecC++/slides/Modele_Bezpieczeństwa.pdf differ diff --git a/C_secure_coding/SecC++/slides/Modelowanie_Zagrożeń.pdf b/C_secure_coding/SecC++/slides/Modelowanie_Zagrożeń.pdf new file mode 100644 index 0000000..5d5b6d9 Binary files /dev/null and b/C_secure_coding/SecC++/slides/Modelowanie_Zagrożeń.pdf differ diff --git a/C_secure_coding/SecC++/wrap_function/Makefile b/C_secure_coding/SecC++/wrap_function/Makefile new file mode 100644 index 0000000..70daea3 --- /dev/null +++ b/C_secure_coding/SecC++/wrap_function/Makefile @@ -0,0 +1,27 @@ +# Przykład "owinięcia" (wrap) / sastąpienia +# pojedynczej funkcji przez linker +# Uwaga na zamierzone włączenie #include "foo.c" +# w test/foo.c +.PHONY := clean + +CC := gcc + +CFLAGS := -Wall -Wextra -pedantic +LDFLAGS := +OBJS := main.o foo.o + +WRAP_CFLAGS := $(CFLAGS) +WRAP_LDFLAGS := $(LD_FLAGS) -Wl,-wrap=foo +WRAP_OBJS := main.o +WRAP_SRCDIR := tests + +all: main wrap + +main: $(OBJS) + $(CC) $(LDFLAGS) -o $@ $^ + +wrap: $(WRAP_OBJS) + $(CC) $(WRAP_LDFLAGS) -o wrap $^ $(WRAP_SRCDIR)/foo.c -I$(WRAP_SRCDIR) + +clean: + @rm -f main wrap $(OBJS) $(WRAP_OBJS) > /dev/null 2>&1 diff --git a/C_secure_coding/SecC++/wrap_function/foo.c b/C_secure_coding/SecC++/wrap_function/foo.c new file mode 100644 index 0000000..74784d9 --- /dev/null +++ b/C_secure_coding/SecC++/wrap_function/foo.c @@ -0,0 +1,8 @@ +#include +#include "foo.h" + +static int value = 42; + +void foo() { + printf("Original foo(), value = %d.\n", value); +} diff --git a/C_secure_coding/SecC++/wrap_function/foo.h b/C_secure_coding/SecC++/wrap_function/foo.h new file mode 100644 index 0000000..7430abb --- /dev/null +++ b/C_secure_coding/SecC++/wrap_function/foo.h @@ -0,0 +1,3 @@ +#pragma once + +void foo(void); diff --git a/C_secure_coding/SecC++/wrap_function/main.c b/C_secure_coding/SecC++/wrap_function/main.c new file mode 100644 index 0000000..a936e40 --- /dev/null +++ b/C_secure_coding/SecC++/wrap_function/main.c @@ -0,0 +1,6 @@ +#include +#include "foo.h" + +int main(void) { + foo(); +} diff --git a/C_secure_coding/SecC++/wrap_function/tests/foo.c b/C_secure_coding/SecC++/wrap_function/tests/foo.c new file mode 100644 index 0000000..410bc16 --- /dev/null +++ b/C_secure_coding/SecC++/wrap_function/tests/foo.c @@ -0,0 +1,9 @@ +#include +#include "../foo.c" /* !!! */ +#include "foo.h" + +void __wrap_foo(void) { + printf("Wrap foo(), value = %d.\n", value); + printf(" "); + __real_foo(); +} diff --git a/C_secure_coding/SecC++/wrap_function/tests/foo.h b/C_secure_coding/SecC++/wrap_function/tests/foo.h new file mode 100644 index 0000000..780e4b0 --- /dev/null +++ b/C_secure_coding/SecC++/wrap_function/tests/foo.h @@ -0,0 +1,7 @@ +#pragma once + +extern int value; + +void foo(void); +void __real_foo(void); +void __wrap_foo(void);