|
| 1 | + |
| 2 | +#include "shared.h" |
| 3 | + |
| 4 | +typedef unsigned long size_t; |
| 5 | + |
| 6 | +namespace std |
| 7 | +{ |
| 8 | + template<class charT> struct char_traits; |
| 9 | + |
| 10 | + typedef size_t streamsize; |
| 11 | + |
| 12 | + template <class T> class allocator { |
| 13 | + public: |
| 14 | + allocator() throw(); |
| 15 | + }; |
| 16 | + |
| 17 | + template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
| 18 | + class basic_string { |
| 19 | + public: |
| 20 | + explicit basic_string(const Allocator& a = Allocator()); |
| 21 | + basic_string(const charT* s, const Allocator& a = Allocator()); |
| 22 | + |
| 23 | + const charT* c_str() const; |
| 24 | + }; |
| 25 | + |
| 26 | + typedef basic_string<char> string; |
| 27 | + |
| 28 | + template <class charT, class traits = char_traits<charT> > |
| 29 | + class basic_istream /*: virtual public basic_ios<charT,traits> - not needed for this test */ { |
| 30 | + public: |
| 31 | + basic_istream<charT,traits>& operator>>(int& n); |
| 32 | + }; |
| 33 | + |
| 34 | + template <class charT, class traits = char_traits<charT> > |
| 35 | + class basic_ostream /*: virtual public basic_ios<charT,traits> - not needed for this test */ { |
| 36 | + public: |
| 37 | + typedef charT char_type; |
| 38 | + basic_ostream<charT,traits>& write(const char_type* s, streamsize n); |
| 39 | + |
| 40 | + basic_ostream<charT, traits>& operator<<(int n); |
| 41 | + }; |
| 42 | + |
| 43 | + template<class charT, class traits> basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const charT*); |
| 44 | + template<class charT, class traits, class Allocator> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str); |
| 45 | + |
| 46 | + template<class charT, class traits = char_traits<charT>> |
| 47 | + class basic_iostream : public basic_istream<charT, traits>, public basic_ostream<charT, traits> { |
| 48 | + public: |
| 49 | + }; |
| 50 | + |
| 51 | + template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT>> |
| 52 | + class basic_stringstream : public basic_iostream<charT, traits> { |
| 53 | + public: |
| 54 | + explicit basic_stringstream(/*ios_base::openmode which = ios_base::out|ios_base::in - not needed for this test*/); |
| 55 | + |
| 56 | + basic_string<charT, traits, Allocator> str() const; |
| 57 | + }; |
| 58 | + |
| 59 | + using stringstream = basic_stringstream<char>; |
| 60 | +} |
| 61 | + |
| 62 | +char *source() { return getenv("USERDATA"); } |
| 63 | +void sink(const std::string &s) {}; |
| 64 | +void sink(const std::stringstream &s) {}; |
| 65 | + |
| 66 | +void test_string() |
| 67 | +{ |
| 68 | + char *a = source(); |
| 69 | + std::string b("123"); |
| 70 | + std::string c(source()); |
| 71 | + |
| 72 | + sink(a); // tainted |
| 73 | + sink(b); |
| 74 | + sink(c); // tainted [NOT DETECTED] |
| 75 | + sink(b.c_str()); |
| 76 | + sink(c.c_str()); // tainted [NOT DETECTED] |
| 77 | +} |
| 78 | + |
| 79 | +void test_stringstream() |
| 80 | +{ |
| 81 | + std::stringstream ss1, ss2, ss3, ss4, ss5; |
| 82 | + std::string t(source()); |
| 83 | + |
| 84 | + ss1 << "1234"; |
| 85 | + ss2 << source(); |
| 86 | + ss3 << "123" << source(); |
| 87 | + ss4 << source() << "456"; |
| 88 | + ss5 << t; |
| 89 | + |
| 90 | + sink(ss1); |
| 91 | + sink(ss2); // tainted [NOT DETECTED] |
| 92 | + sink(ss3); // tainted [NOT DETECTED] |
| 93 | + sink(ss4); // tainted [NOT DETECTED] |
| 94 | + sink(ss5); // tainted [NOT DETECTED] |
| 95 | + sink(ss1.str()); |
| 96 | + sink(ss2.str()); // tainted [NOT DETECTED] |
| 97 | + sink(ss3.str()); // tainted [NOT DETECTED] |
| 98 | + sink(ss4.str()); // tainted [NOT DETECTED] |
| 99 | + sink(ss5.str()); // tainted [NOT DETECTED] |
| 100 | +} |
| 101 | + |
| 102 | +void test_stringstream_int(int source) |
| 103 | +{ |
| 104 | + std::stringstream ss1, ss2; |
| 105 | + |
| 106 | + ss1 << 1234; |
| 107 | + ss2 << source; |
| 108 | + |
| 109 | + sink(ss1); |
| 110 | + sink(ss2); // tainted [NOT DETECTED] |
| 111 | + sink(ss1.str()); |
| 112 | + sink(ss2.str()); // tainted [NOT DETECTED] |
| 113 | +} |
| 114 | + |
| 115 | +using namespace std; |
| 116 | + |
| 117 | +char *user_input() { |
| 118 | + return source(); |
| 119 | +} |
| 120 | + |
| 121 | +void sink(const char *filename, const char *mode); |
| 122 | + |
| 123 | +void test_strings2() |
| 124 | +{ |
| 125 | + string path1 = user_input(); |
| 126 | + sink(path1.c_str(), "r"); // tainted [NOT DETECTED] |
| 127 | + |
| 128 | + string path2; |
| 129 | + path2 = user_input(); |
| 130 | + sink(path2.c_str(), "r"); // tainted |
| 131 | + |
| 132 | + string path3(user_input()); |
| 133 | + sink(path3.c_str(), "r"); // tainted [NOT DETECTED] |
| 134 | +} |
| 135 | + |
| 136 | +void test_string3() |
| 137 | +{ |
| 138 | + const char *cs = source(); |
| 139 | + |
| 140 | + // convert char * -> std::string |
| 141 | + std::string ss(cs); |
| 142 | + |
| 143 | + sink(cs); // tainted |
| 144 | + sink(ss); // tainted [NOT DETECTED] |
| 145 | +} |
| 146 | + |
| 147 | +void test_string4() |
| 148 | +{ |
| 149 | + const char *cs = source(); |
| 150 | + |
| 151 | + // convert char * -> std::string |
| 152 | + std::string ss(cs); |
| 153 | + |
| 154 | + // convert back std::string -> char * |
| 155 | + cs = ss.c_str(); |
| 156 | + |
| 157 | + sink(cs); // tainted [NOT DETECTED] |
| 158 | + sink(ss); // tainted [NOT DETECTED] |
| 159 | +} |
0 commit comments