libsqreen 0.3.0.0.3 → 0.6.1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,207 @@
1
+ #ifndef _GNU_SOURCE
2
+ #define DEFINED_GNU_SOURCE
3
+ // for vaspritnf
4
+ #define _GNU_SOURCE
5
+ #endif
6
+ #include <stdio.h>
7
+ #ifdef DEFINED_GNU_SOURCE
8
+ #undef _GNU_SOURCE
9
+ #endif
10
+
11
+ #include "logging.h"
12
+ #include <ruby.h>
13
+ #include <string.h>
14
+
15
+ #ifdef _WIN32
16
+ # define DIR_SEP '\\'
17
+ #else
18
+ # define DIR_SEP '/'
19
+ #endif
20
+
21
+ extern VALUE waf_mod;
22
+ PW_LOG_LEVEL log_threshold;
23
+ static int file_strip_idx;
24
+
25
+ void
26
+ log_init() {
27
+ const char *file = __FILE__;
28
+ size_t file_len = strlen(file);
29
+
30
+ const char *loc;
31
+ for (loc = file + file_len - 1; loc >= file; loc--) {
32
+ if (*loc == DIR_SEP) {
33
+ break;
34
+ }
35
+ }
36
+ file_strip_idx = loc >= file ? (int)(loc - file + 1) : 0;
37
+ }
38
+
39
+ static void
40
+ on_log(PW_LOG_LEVEL level, const char *function, const char *file,
41
+ int line, const char *message, size_t message_len);
42
+
43
+
44
+ static VALUE log_level_to_fixnum(PW_LOG_LEVEL level) {
45
+ VALUE sym;
46
+
47
+ switch (level) {
48
+ case PWL_TRACE:
49
+ sym = INT2FIX(0); // Logger::DEBUG
50
+ break;
51
+ case PWL_DEBUG:
52
+ sym = INT2FIX(0); // Logger::DEBUG
53
+ break;
54
+ case PWL_INFO:
55
+ sym = INT2FIX(1); // Logger::INFO
56
+ break;
57
+ case PWL_WARN:
58
+ sym = INT2FIX(2); // Logger::WARN
59
+ break;
60
+ case PWL_ERROR:
61
+ sym = INT2FIX(3); // Logger::ERROR
62
+ break;
63
+ default:
64
+ rb_raise(rb_eArgError, "not valid value");
65
+ }
66
+
67
+ return sym;
68
+ }
69
+
70
+ static PW_LOG_LEVEL
71
+ sym_to_log_level(VALUE sym) {
72
+ PW_LOG_LEVEL level;
73
+
74
+ Check_Type(sym, T_SYMBOL);
75
+
76
+ if (SYM2ID(sym) == rb_intern("trace")) {
77
+ level = PWL_TRACE;
78
+ } else if (SYM2ID(sym) == rb_intern("debug")) {
79
+ level = PWL_DEBUG;
80
+ } else if (SYM2ID(sym) == rb_intern("info")) {
81
+ level = PWL_INFO;
82
+ } else if (SYM2ID(sym) == rb_intern("warn")) {
83
+ level = PWL_WARN;
84
+ } else if (SYM2ID(sym) == rb_intern("error")) {
85
+ level = PWL_ERROR;
86
+ } else {
87
+ rb_raise(rb_eArgError, "not valid value");
88
+ }
89
+
90
+ return level;
91
+ }
92
+
93
+ static VALUE
94
+ call_logger_add(VALUE ary) {
95
+ VALUE logger = rb_ary_entry(ary, 0);
96
+ VALUE severity = rb_ary_entry(ary, 1);
97
+ VALUE log_msg = rb_ary_entry(ary, 2);
98
+
99
+ return rb_funcall(logger, rb_intern("add"), 2, severity, log_msg);
100
+ }
101
+
102
+
103
+ VALUE
104
+ libsqreen_waf_log_enable(VALUE self, VALUE severity) {
105
+ (void)self;
106
+ PW_LOG_LEVEL level;
107
+
108
+ Check_Type(severity, T_SYMBOL);
109
+
110
+ level = sym_to_log_level(severity);
111
+ powerwaf_setupLogging(on_log, level);
112
+ log_threshold = level;
113
+
114
+ return Qnil;
115
+ }
116
+
117
+ VALUE
118
+ libsqreen_waf_log_disable(VALUE self) {
119
+ (void)self;
120
+ PW_LOG_LEVEL level = PWL_ERROR;
121
+
122
+ powerwaf_setupLogging(NULL, level);
123
+ log_threshold = _PWL_AFTER_LAST;
124
+
125
+ return Qnil;
126
+ }
127
+
128
+ VALUE
129
+ libsqreen_waf_set_logger(VALUE self, VALUE logger) {
130
+ ID i_logger;
131
+
132
+ if (logger == Qnil) {
133
+ libsqreen_waf_log_disable(self);
134
+ }
135
+
136
+ i_logger = rb_intern("@logger");
137
+ rb_ivar_set(self, i_logger, logger);
138
+
139
+ if (logger != Qnil) {
140
+ libsqreen_waf_log_enable(self, ID2SYM(rb_intern("error")));
141
+ }
142
+
143
+ return logger;
144
+ }
145
+
146
+ VALUE
147
+ libsqreen_waf_get_logger(VALUE self) {
148
+ VALUE logger;
149
+ ID i_logger;
150
+
151
+ i_logger = rb_intern("@logger");
152
+ logger = rb_ivar_get(self, i_logger);
153
+
154
+ return logger;
155
+ }
156
+
157
+ static void
158
+ on_log(PW_LOG_LEVEL level,
159
+ const char *function,
160
+ const char *file,
161
+ int line,
162
+ const char *message, size_t message_len) {
163
+ VALUE severity;
164
+ VALUE logger;
165
+ VALUE log_msg;
166
+
167
+ logger = libsqreen_waf_get_logger(waf_mod);
168
+ if (logger == Qnil) {
169
+ return;
170
+ }
171
+
172
+ severity = log_level_to_fixnum(level);
173
+ if (severity == Qnil) {
174
+ return;
175
+ }
176
+
177
+ if (message_len > INT_MAX) {
178
+ return;
179
+ }
180
+
181
+ log_msg = rb_sprintf("from %s:%d:in `%s': %.*s", file, line, function,
182
+ (int)message_len, message);
183
+
184
+ VALUE log_args = rb_ary_new3(3L, logger, severity, log_msg);
185
+ (void) rb_protect(call_logger_add, log_args, &(int) { 0 });
186
+ }
187
+
188
+ void
189
+ ruby_log(PW_LOG_LEVEL level,
190
+ const char *function,
191
+ const char *file,
192
+ int line,
193
+ const char *fmt, ...)
194
+ {
195
+ char *message = NULL;
196
+ va_list ap;
197
+ va_start(ap, fmt);
198
+ int message_len = vasprintf(&message, fmt, ap);
199
+ va_end(ap);
200
+
201
+ if (!message) {
202
+ return;
203
+ }
204
+ on_log(level, function, file + file_strip_idx, line,
205
+ message, (size_t)message_len);
206
+ free(message);
207
+ }
@@ -0,0 +1,29 @@
1
+ #ifndef LOGGING_H
2
+ #define LOGGING_H
3
+
4
+ #include <waf.h>
5
+ #include <stdlib.h>
6
+ #include <ruby.h>
7
+
8
+ void log_init(void);
9
+
10
+ extern PW_LOG_LEVEL log_threshold;
11
+ // TODO: skip call if the log level is too low
12
+ #define RUBY_LOG(level, fmt, ...) \
13
+ do { \
14
+ if (level >= log_threshold) { \
15
+ ruby_log(level, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \
16
+ } \
17
+ } while(0)
18
+
19
+ void ruby_log(PW_LOG_LEVEL level, const char *function, const char *file,
20
+ int line, const char *fmt, ...)
21
+ __attribute__((format (printf, 5, 6)));
22
+
23
+ // ruby methods
24
+ VALUE libsqreen_waf_log_enable(VALUE self, VALUE severity);
25
+ VALUE libsqreen_waf_log_disable(VALUE self);
26
+ VALUE libsqreen_waf_set_logger(VALUE self, VALUE logger);
27
+ VALUE libsqreen_waf_get_logger(VALUE self);
28
+
29
+ #endif // LOGGING_H
@@ -0,0 +1,2 @@
1
+ void Init_libsqreen_extension(void);
2
+ void Init_libsqreen_extension() {}
@@ -4,22 +4,15 @@
4
4
  require 'libsqreen/version'
5
5
 
6
6
  module LibSqreen
7
- (@load_order ||= []).push(:'libsqreen.rb')
8
-
9
7
  def self.extension_path
10
8
  RUBY_VERSION =~ /^(\d+\.\d+)/ && "ext/#{RUBY_PLATFORM}/#{$1}"
11
9
  end
12
10
 
13
11
  def self.require_extension
14
- begin
15
- require "#{extension_path}/libsqreen.so"
16
- rescue LoadError
17
- require "libsqreen.so"
18
- end
19
12
  begin
20
13
  require "#{extension_path}/libsqreen_extension.so"
21
14
  rescue LoadError
22
- require "libsqreen_extension.so"
15
+ require 'libsqreen_extension.so'
23
16
  end
24
17
  end
25
18
 
@@ -2,5 +2,5 @@
2
2
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
3
 
4
4
  module LibSqreen
5
- VERSION = "0.3.0.0.3"
5
+ VERSION = "0.6.1.0.0"
6
6
  end
@@ -13,17 +13,27 @@ Gem::Specification.new do |s|
13
13
  s.license = "Sqreen"
14
14
 
15
15
  s.files = `git ls-files`.split("\n")
16
+ .reject { |f| f =~ /^release\// }
17
+ .reject { |f| f =~ /\.gitkeep$/ }
18
+ .reject { |f| f =~ /^rakefile_bins\.rb$/ }
19
+ .reject { |f| f =~ /^valgrind\.supp$/ }
16
20
  .reject { |f| f =~ /^release\// }
17
21
  .reject { |f| f =~ /^vendor\// }
18
22
  .reject { |f| f =~ /^.git\// }
19
23
  .reject { |f| f =~ /^docker\// }
24
+ .reject { |f| f =~ /^ruby_pkgs\// }
25
+ .reject { |f| f =~ /^azure-/ }
26
+ .reject { |f| f =~ /^\.git/ }
27
+ .reject { |f| %w{Makefile Rakefile s3get}.include?(f) }
20
28
  .reject { |f| f =~ /^test\// } + Dir.glob('vendor/lib*/**/*')
21
29
 
22
30
  s.require_paths = ["lib"]
23
31
  s.require_paths += ["lib/ext"]
24
- s.extensions = ["ext/libsqreen/extconf.rb", "ext/libsqreen_extension/extconf.rb"]
32
+ s.extensions = ["ext/libsqreen_extension/extconf.rb"]
25
33
 
26
34
  s.add_development_dependency 'rake', '~> 11.0'
27
35
  s.add_development_dependency 'rake-compiler', '~> 0'
36
+ s.add_development_dependency 'minitest', '5.11.3'
37
+ s.add_development_dependency 'minitest-junit', '~> 0.2.0'
28
38
  end
29
39
 
@@ -14,7 +14,10 @@ extern "C" {
14
14
  #include <stdbool.h>
15
15
  #include <stdlib.h>
16
16
 
17
- #define MAX_REGEX_STRING_LENGTH 4096
17
+ #define PW_MAX_STRING_LENGTH 4096
18
+ #define PW_MAX_MAP_DEPTH 20
19
+ #define PW_MAX_ARRAY_LENGTH 256
20
+ #define PW_RUN_TIMEOUT 5000
18
21
 
19
22
  typedef enum
20
23
  {
@@ -26,14 +29,28 @@ typedef enum
26
29
  PWI_MAP = 1 << 4, // `value` shall be decoded as an array of PWArgs of length `nbEntries`, each item having a `parameterName`
27
30
  } PW_INPUT_TYPE;
28
31
 
29
- typedef struct
32
+ typedef struct _PWArgs PWArgs;
33
+
34
+ struct _PWArgs
30
35
  {
31
36
  const char * parameterName;
32
37
  uint64_t parameterNameLength;
33
- const void * value;
38
+ union {
39
+ const char * stringValue;
40
+ uint64_t uintValue;
41
+ int64_t intValue;
42
+ const PWArgs * array;
43
+ const void * rawHandle;
44
+ };
34
45
  uint64_t nbEntries;
35
46
  PW_INPUT_TYPE type;
36
- } PWArgs;
47
+ };
48
+
49
+ typedef struct
50
+ {
51
+ uint64_t maxArrayLength;
52
+ uint64_t maxMapDepth;
53
+ } PWConfig;
37
54
 
38
55
  /// InitializePowerWAF
39
56
  ///
@@ -43,9 +60,10 @@ typedef struct
43
60
  ///
44
61
  /// @param ruleName Name the atom that provided the patterns we're about to initialize with
45
62
  /// @param wafRule JSON blob containing the patterns to work with
63
+ /// @param config Customized limits for the PWArgs validation
46
64
  /// @return The success (true) or faillure (false) of the init
47
65
 
48
- extern bool powerwaf_initializePowerWAF(const char * ruleName, const char * wafRule);
66
+ extern bool powerwaf_init(const char * ruleName, const char * wafRule, const PWConfig * config);
49
67
 
50
68
 
51
69
  typedef enum
@@ -62,7 +80,7 @@ typedef enum
62
80
  PWD_DUPLICATE_FLOW_STEP,
63
81
  } PW_DIAG_CODE;
64
82
 
65
- /// powerwaf_initializePowerWAFWithDiag
83
+ /// powerwaf_initWithDiag
66
84
  ///
67
85
  /// Initialize a rule in the PowerWAF
68
86
  /// Must be called before calling RunPowerWAF on this rule name
@@ -71,15 +89,16 @@ typedef enum
71
89
  ///
72
90
  /// @param ruleName Name the atom that provided the patterns we're about to initialize with
73
91
  /// @param wafRule JSON blob containing the patterns to work with
92
+ /// @param config Customized limits for the PWArgs validation. NULL or a value of 0 mean using the default value described above
74
93
  /// @param errors Pointer to the pointer to be populated with a potential error report. Set to NULL not to generate such a report
75
94
  /// @return The success (true) or faillure (false) of the init
76
95
 
77
96
 
78
- extern bool powerwaf_initializePowerWAFWithDiag(const char * ruleName, const char * wafRule, char ** errors);
97
+ extern bool powerwaf_initWithDiag(const char * ruleName, const char * wafRule, const PWConfig * config, char ** errors);
79
98
 
80
99
  /// powerwaf_freeDiagnotics
81
100
  ///
82
- /// Free the error report generated by powerwaf_initializePowerWAFWithDiag
101
+ /// Free the error report generated by powerwaf_initWithDiag
83
102
  ///
84
103
  /// @param errors Pointer to a populated error report. NULL will be safely ignored
85
104
 
@@ -126,8 +145,8 @@ typedef struct
126
145
  ///
127
146
  /// Threading guarantees: When calling this API, a lock will be taken for a very short window as this call will take ownership of a shared smart pointer.
128
147
  /// This pointer implement reference counting and can be owned by as many thread as you want.
129
- /// If you call powerwaf_initializePowerWAF while evaluation of powerwaf_runPowerWAF is ongoing, the calls having already taken ownership will safely finish processing.
130
- /// The shared pointer will be destroyed, without locking powerwaf_initializePowerWAF, when the last powerwaf_runPowerWAF finish processing.
148
+ /// If you call powerwaf_init while evaluation of powerwaf_run is ongoing, the calls having already taken ownership will safely finish processing.
149
+ /// The shared pointer will be destroyed, without locking powerwaf_init, when the last powerwaf_run finish processing.
131
150
  ///
132
151
  /// Maximum budget: The budget is internally stored in nanoseconds in an int64_t variable. This is then added to the current time, also coded in nano seconds.
133
152
  /// Due to those convertions, the maximum safe value for the next 15 years is 2^52. After that, 2^51.
@@ -137,7 +156,7 @@ typedef struct
137
156
  /// @param timeLeftInUs The maximum time in microsecond PowerWAF is allowed to take
138
157
  /// @return Whether the pattern matched or whether we encountered an error
139
158
 
140
- extern PWRet * powerwaf_runPowerWAF(const char * ruleName, const PWArgs * parameters, size_t timeLeftInUs);
159
+ extern PWRet * powerwaf_run(const char * ruleName, const PWArgs * parameters, size_t timeLeftInUs);
141
160
 
142
161
 
143
162
  typedef struct {
@@ -152,7 +171,7 @@ typedef struct {
152
171
  ///
153
172
  /// @return The API version in SemVer form
154
173
 
155
- extern const PWVersion powerwaf_getVersion(void);
174
+ extern PWVersion powerwaf_getVersion(void);
156
175
 
157
176
 
158
177
  typedef enum
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libsqreen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.0.3
4
+ version: 0.6.1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sqreen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-07 00:00:00.000000000 Z
11
+ date: 2020-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,36 +38,56 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 5.11.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 5.11.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-junit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.2.0
41
69
  description: Distributes fast compiled functions for the Sqreen agent
42
70
  email:
43
71
  - contact@sqreen.com
44
72
  executables: []
45
73
  extensions:
46
- - ext/libsqreen/extconf.rb
47
74
  - ext/libsqreen_extension/extconf.rb
48
75
  extra_rdoc_files: []
49
76
  files:
50
- - ".gitignore"
51
- - ".gitmodules"
52
77
  - CHANGELOG.md
53
78
  - Gemfile
54
79
  - LICENSE
55
- - Makefile
56
80
  - README.md
57
- - Rakefile
58
- - azure-pipelines.yml
59
- - ext/libsqreen/arch.rb
60
- - ext/libsqreen/extconf.rb
61
- - ext/libsqreen/libsqreen.c
62
- - ext/libsqreen/location.rb
63
- - ext/libsqreen/paths.rb
81
+ - ext/env_overrides.rb
64
82
  - ext/libsqreen_extension/extconf.rb
65
83
  - ext/libsqreen_extension/libsqreen_extension.c
66
84
  - ext/libsqreen_extension/libsqreen_extension.version
85
+ - ext/libsqreen_extension/logging.c
86
+ - ext/libsqreen_extension/logging.h
87
+ - ext/libsqreen_extension/stub/libsqreen_stub.c
67
88
  - lib/libsqreen.rb
68
89
  - lib/libsqreen/version.rb
69
90
  - libsqreen.gemspec
70
- - s3get
71
91
  - vendor/libc++/LICENSE.libc++.txt
72
92
  - vendor/libc++/LICENSE.libunwind.txt
73
93
  - vendor/libc++/x86_64/linux/libc++.a