Message ID | 20210221154919.68050-1-john.wood@gmx.com |
---|---|
Headers | show |
Series | Fork brute force attack mitigation | expand |
Hi-- On 2/21/21 7:49 AM, John Wood wrote: > > Signed-off-by: John Wood <john.wood@gmx.com> > --- > security/brute/brute.c | 488 +++++++++++++++++++++++++++++++++++++++-- > 1 file changed, 474 insertions(+), 14 deletions(-) > > diff --git a/security/brute/brute.c b/security/brute/brute.c > index 70f812bb7763..645bd6e02638 100644 > --- a/security/brute/brute.c > +++ b/security/brute/brute.c > +/** > + * print_fork_attack_running() - Warn about a fork brute force attack. > + */ > +static inline void print_fork_attack_running(void) > +{ > + pr_warn("Fork brute force attack detected [%s]\n", current->comm); > +} Do these pr_warn() calls need to be rate-limited so that they don't flood the kernel log? > +/** > + * print_exec_attack_running() - Warn about an exec brute force attack. > + * @stats: Statistical data shared by all the fork hierarchy processes. > + * > + * The statistical data shared by all the fork hierarchy processes cannot be > + * NULL. > + * > + * Before showing the process name it is mandatory to find a process that holds > + * a pointer to the exec statistics. > + * > + * Context: Must be called with tasklist_lock and brute_stats_ptr_lock held. > + */ > +static void print_exec_attack_running(const struct brute_stats *stats) > +{ > + struct task_struct *p; > + struct brute_stats **p_stats; > + bool found = false; > + > + for_each_process(p) { > + p_stats = brute_stats_ptr(p); > + if (*p_stats == stats) { > + found = true; > + break; > + } > } > + > + if (WARN(!found, "No exec process\n")) > + return; > + > + pr_warn("Exec brute force attack detected [%s]\n", p->comm); > +} thanks. -- ~Randy
Hi, one spello in 2 locations: On 2/21/21 7:49 AM, John Wood wrote: > To detect a brute force attack it is necessary that the statistics > shared by all the fork hierarchy processes be updated in every fatal > crash and the most important data to update is the application crash > period. To do so, use the new "task_fatal_signal" LSM hook added in a > previous step. > > The application crash period must be a value that is not prone to change > due to spurious data and follows the real crash period. So, to compute > it, the exponential moving average (EMA) is used. > > There are two types of brute force attacks that need to be detected. The > first one is an attack that happens through the fork system call and the > second one is an attack that happens through the execve system call. The > first type uses the statistics shared by all the fork hierarchy > processes, but the second type cannot use this statistical data due to > these statistics dissapear when the involved tasks finished. In this disappear > last scenario the attack info should be tracked by the statistics of a > higher fork hierarchy (the hierarchy that contains the process that > forks before the execve system call). > > Moreover, these two attack types have two variants. A slow brute force > attack that is detected if the maximum number of faults per fork > hierarchy is reached and a fast brute force attack that is detected if > the application crash period falls below a certain threshold. > > Also, this patch adds locking to protect the statistics pointer hold by > every process. > > Signed-off-by: John Wood <john.wood@gmx.com> > --- > security/brute/brute.c | 488 +++++++++++++++++++++++++++++++++++++++-- > 1 file changed, 474 insertions(+), 14 deletions(-) > > diff --git a/security/brute/brute.c b/security/brute/brute.c > index 70f812bb7763..645bd6e02638 100644 > --- a/security/brute/brute.c > +++ b/security/brute/brute.c > +/** > + * brute_get_exec_stats() - Get the exec statistics. > + * @stats: When this function is called, this parameter must point to the > + * current process' statistical data. When this function returns, this > + * parameter points to the parent process' statistics of the fork > + * hierarchy that hold the current process' statistics. > + * > + * To manage a brute force attack that happens through the execve system call it > + * is not possible to use the statistical data hold by this process due to these > + * statistics dissapear when this task is finished. In this scenario this data disappear > + * should be tracked by the statistics of a higher fork hierarchy (the hierarchy > + * that contains the process that forks before the execve system call). > + * > + * To find these statistics the current fork hierarchy must be traversed up > + * until new statistics are found. > + * > + * Context: Must be called with tasklist_lock and brute_stats_ptr_lock held. > + */ > +static void brute_get_exec_stats(struct brute_stats **stats) > +{ -- ~Randy
Hi-- scripts/kernel-doc does not like these items to be marked as being in kernel-doc notation. scripts/kernel-doc does not recognize them as one of: struct, union, enum, typedef, so it defaults to trying to interpret these as functions, and then says: (I copied these blocks to my test megatest.c source file.) ../src/megatest.c:1214: warning: cannot understand function prototype: 'const u64 BRUTE_EMA_WEIGHT_NUMERATOR = 7; ' ../src/megatest.c:1219: warning: cannot understand function prototype: 'const u64 BRUTE_EMA_WEIGHT_DENOMINATOR = 10; ' ../src/megatest.c:1228: warning: cannot understand function prototype: 'const unsigned char BRUTE_MAX_FAULTS = 200; ' ../src/megatest.c:1239: warning: cannot understand function prototype: 'const unsigned char BRUTE_MIN_FAULTS = 5; ' ../src/megatest.c:1249: warning: cannot understand function prototype: 'const u64 BRUTE_CRASH_PERIOD_THRESHOLD = 30000; ' On 2/21/21 7:49 AM, John Wood wrote: > > +/** > + * brute_stats_ptr_lock - Lock to protect the brute_stats structure pointer. > + */ > +static DEFINE_RWLOCK(brute_stats_ptr_lock); > +/** > + * BRUTE_EMA_WEIGHT_NUMERATOR - Weight's numerator of EMA. > + */ > +static const u64 BRUTE_EMA_WEIGHT_NUMERATOR = 7; > +/** > + * BRUTE_EMA_WEIGHT_DENOMINATOR - Weight's denominator of EMA. > + */ > +static const u64 BRUTE_EMA_WEIGHT_DENOMINATOR = 10; > +/** > + * BRUTE_MAX_FAULTS - Maximum number of faults. > + * > + * If a brute force attack is running slowly for a long time, the application > + * crash period's EMA is not suitable for the detection. This type of attack > + * must be detected using a maximum number of faults. > + */ > +static const unsigned char BRUTE_MAX_FAULTS = 200; > +/** > + * BRUTE_MIN_FAULTS - Minimum number of faults. > + * > + * The application crash period's EMA cannot be used until a minimum number of > + * data has been applied to it. This constraint allows getting a trend when this > + * moving average is used. Moreover, it avoids the scenario where an application > + * fails quickly from execve system call due to reasons unrelated to a real > + * attack. > + */ > +static const unsigned char BRUTE_MIN_FAULTS = 5; > +/** > + * BRUTE_CRASH_PERIOD_THRESHOLD - Application crash period threshold. > + * > + * The units are expressed in milliseconds. > + * > + * A fast brute force attack is detected when the application crash period falls > + * below this threshold. > + */ > +static const u64 BRUTE_CRASH_PERIOD_THRESHOLD = 30000; Basically we don't support scalars in kernel-doc notation... -- ~Randy
Hi, On Sun, Feb 21, 2021 at 06:25:51PM -0800, Randy Dunlap wrote: > Hi-- > > On 2/21/21 7:49 AM, John Wood wrote: > > > > +/** > > + * print_fork_attack_running() - Warn about a fork brute force attack. > > + */ > > +static inline void print_fork_attack_running(void) > > +{ > > + pr_warn("Fork brute force attack detected [%s]\n", current->comm); > > +} > > Do these pr_warn() calls need to be rate-limited so that they don't > flood the kernel log? I think it is not necessary since when a brute force attack through the fork system call is detected, a fork warning appears only once. Then, all the offending tasks involved in the attack are killed. But if the parent try to run again the same app already killed, a new crash will trigger a brute force attack through the execve system call, then this parent is killed, and a new warning message appears. Now, the parent and childs are killed, the attacks are mitigated and only a few messages (one or two) have been shown in the kernel log. Thanks, John Wood > > +/** > > + * print_exec_attack_running() - Warn about an exec brute force attack. > > + * @stats: Statistical data shared by all the fork hierarchy processes. > > + * > > + * The statistical data shared by all the fork hierarchy processes cannot be > > + * NULL. > > + * > > + * Before showing the process name it is mandatory to find a process that holds > > + * a pointer to the exec statistics. > > + * > > + * Context: Must be called with tasklist_lock and brute_stats_ptr_lock held. > > + */ > > +static void print_exec_attack_running(const struct brute_stats *stats) > > +{ > > + struct task_struct *p; > > + struct brute_stats **p_stats; > > + bool found = false; > > + > > + for_each_process(p) { > > + p_stats = brute_stats_ptr(p); > > + if (*p_stats == stats) { > > + found = true; > > + break; > > + } > > } > > + > > + if (WARN(!found, "No exec process\n")) > > + return; > > + > > + pr_warn("Exec brute force attack detected [%s]\n", p->comm); > > +} > > > thanks. > -- > ~Randy >
Hi, On Sun, Feb 21, 2021 at 06:47:16PM -0800, Randy Dunlap wrote: > Hi-- > > scripts/kernel-doc does not like these items to be marked > as being in kernel-doc notation. scripts/kernel-doc does not > recognize them as one of: struct, union, enum, typedef, so it > defaults to trying to interpret these as functions, and then > says: > > (I copied these blocks to my test megatest.c source file.) > > > ../src/megatest.c:1214: warning: cannot understand function prototype: 'const u64 BRUTE_EMA_WEIGHT_NUMERATOR = 7; ' > ../src/megatest.c:1219: warning: cannot understand function prototype: 'const u64 BRUTE_EMA_WEIGHT_DENOMINATOR = 10; ' > ../src/megatest.c:1228: warning: cannot understand function prototype: 'const unsigned char BRUTE_MAX_FAULTS = 200; ' > ../src/megatest.c:1239: warning: cannot understand function prototype: 'const unsigned char BRUTE_MIN_FAULTS = 5; ' > ../src/megatest.c:1249: warning: cannot understand function prototype: 'const u64 BRUTE_CRASH_PERIOD_THRESHOLD = 30000; ' > > > On 2/21/21 7:49 AM, John Wood wrote: > > > > +/** > > + * brute_stats_ptr_lock - Lock to protect the brute_stats structure pointer. > > + */ > > +static DEFINE_RWLOCK(brute_stats_ptr_lock); > > > +/** > > + * BRUTE_EMA_WEIGHT_NUMERATOR - Weight's numerator of EMA. > > + */ > > +static const u64 BRUTE_EMA_WEIGHT_NUMERATOR = 7; > > > +/** > > + * BRUTE_EMA_WEIGHT_DENOMINATOR - Weight's denominator of EMA. > > + */ > > +static const u64 BRUTE_EMA_WEIGHT_DENOMINATOR = 10; > > > +/** > > + * BRUTE_MAX_FAULTS - Maximum number of faults. > > + * > > + * If a brute force attack is running slowly for a long time, the application > > + * crash period's EMA is not suitable for the detection. This type of attack > > + * must be detected using a maximum number of faults. > > + */ > > +static const unsigned char BRUTE_MAX_FAULTS = 200; > > > +/** > > + * BRUTE_MIN_FAULTS - Minimum number of faults. > > + * > > + * The application crash period's EMA cannot be used until a minimum number of > > + * data has been applied to it. This constraint allows getting a trend when this > > + * moving average is used. Moreover, it avoids the scenario where an application > > + * fails quickly from execve system call due to reasons unrelated to a real > > + * attack. > > + */ > > +static const unsigned char BRUTE_MIN_FAULTS = 5; > > > +/** > > + * BRUTE_CRASH_PERIOD_THRESHOLD - Application crash period threshold. > > + * > > + * The units are expressed in milliseconds. > > + * > > + * A fast brute force attack is detected when the application crash period falls > > + * below this threshold. > > + */ > > +static const u64 BRUTE_CRASH_PERIOD_THRESHOLD = 30000; > > Basically we don't support scalars in kernel-doc notation... So, to keep it commented it would be better to use a normal comment block? /* * Documentation here */ What do you think? Thanks, John Wood > -- > ~Randy >
Hi, On Sun, Feb 21, 2021 at 06:30:10PM -0800, Randy Dunlap wrote: > Hi, > > one spello in 2 locations: > > On 2/21/21 7:49 AM, John Wood wrote: > [...] > > these statistics dissapear when the involved tasks finished. In this > > disappear > [...] > > + * statistics dissapear when this task is finished. In this scenario this data > > disappear > [...] This typos will be corrected in the next version. Thanks a lot, John Wood > > -- > ~Randy >
On 2/23/21 10:20 AM, John Wood wrote: > Hi, > > On Sun, Feb 21, 2021 at 06:47:16PM -0800, Randy Dunlap wrote: >> Hi-- >> >> scripts/kernel-doc does not like these items to be marked >> as being in kernel-doc notation. scripts/kernel-doc does not >> recognize them as one of: struct, union, enum, typedef, so it >> defaults to trying to interpret these as functions, and then >> says: >> >> (I copied these blocks to my test megatest.c source file.) >> >> >> ../src/megatest.c:1214: warning: cannot understand function prototype: 'const u64 BRUTE_EMA_WEIGHT_NUMERATOR = 7; ' >> ../src/megatest.c:1219: warning: cannot understand function prototype: 'const u64 BRUTE_EMA_WEIGHT_DENOMINATOR = 10; ' >> ../src/megatest.c:1228: warning: cannot understand function prototype: 'const unsigned char BRUTE_MAX_FAULTS = 200; ' >> ../src/megatest.c:1239: warning: cannot understand function prototype: 'const unsigned char BRUTE_MIN_FAULTS = 5; ' >> ../src/megatest.c:1249: warning: cannot understand function prototype: 'const u64 BRUTE_CRASH_PERIOD_THRESHOLD = 30000; ' >> >> >> On 2/21/21 7:49 AM, John Wood wrote: >>> >>> +/** >>> + * brute_stats_ptr_lock - Lock to protect the brute_stats structure pointer. >>> + */ >>> +static DEFINE_RWLOCK(brute_stats_ptr_lock); >> >>> +/** >>> + * BRUTE_EMA_WEIGHT_NUMERATOR - Weight's numerator of EMA. >>> + */ >>> +static const u64 BRUTE_EMA_WEIGHT_NUMERATOR = 7; >> >>> +/** >>> + * BRUTE_EMA_WEIGHT_DENOMINATOR - Weight's denominator of EMA. >>> + */ >>> +static const u64 BRUTE_EMA_WEIGHT_DENOMINATOR = 10; >> >>> +/** >>> + * BRUTE_MAX_FAULTS - Maximum number of faults. >>> + * >>> + * If a brute force attack is running slowly for a long time, the application >>> + * crash period's EMA is not suitable for the detection. This type of attack >>> + * must be detected using a maximum number of faults. >>> + */ >>> +static const unsigned char BRUTE_MAX_FAULTS = 200; >> >>> +/** >>> + * BRUTE_MIN_FAULTS - Minimum number of faults. >>> + * >>> + * The application crash period's EMA cannot be used until a minimum number of >>> + * data has been applied to it. This constraint allows getting a trend when this >>> + * moving average is used. Moreover, it avoids the scenario where an application >>> + * fails quickly from execve system call due to reasons unrelated to a real >>> + * attack. >>> + */ >>> +static const unsigned char BRUTE_MIN_FAULTS = 5; >> >>> +/** >>> + * BRUTE_CRASH_PERIOD_THRESHOLD - Application crash period threshold. >>> + * >>> + * The units are expressed in milliseconds. >>> + * >>> + * A fast brute force attack is detected when the application crash period falls >>> + * below this threshold. >>> + */ >>> +static const u64 BRUTE_CRASH_PERIOD_THRESHOLD = 30000; >> >> Basically we don't support scalars in kernel-doc notation... > > So, to keep it commented it would be better to use a normal comment block? > > /* > * Documentation here > */ > > What do you think? Yes, please, just a normal /* comment block. thanks. -- ~Randy