Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cfg.lex
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ MCAST_TTL "mcast_ttl"
TOS "tos"
DISABLE_DNS_FAILOVER "disable_dns_failover"
REDACT_PII_ "redact_pii_"
REDACT_TEMPLATE "redact_template"
REDACT_MODE "redact_mode"
DISABLE_DNS_BLACKLIST "disable_dns_blacklist"
DST_BLACKLIST "dst_blacklist"
MAX_WHILE_LOOPS "max_while_loops"
Expand Down Expand Up @@ -527,6 +529,10 @@ SPACE [ ]
return DISABLE_DNS_FAILOVER; }
<INITIAL>{REDACT_PII_} { count(); yylval.strval=yytext;
return REDACT_PII_;}
<INITIAL>{REDACT_TEMPLATE} { count(); yylval.strval=yytext;
return REDACT_TEMPLATE;}
<INITIAL>{REDACT_MODE} { count(); yylval.strval=yytext;
return REDACT_MODE;}
<INITIAL>{DISABLE_DNS_BLACKLIST} { count(); yylval.strval=yytext;
return DISABLE_DNS_BLACKLIST; }
<INITIAL>{DST_BLACKLIST} { count(); yylval.strval=yytext;
Expand Down
51 changes: 50 additions & 1 deletion cfg.y
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
#include "config.h"
#include "mem/rpm_mem.h"
#include "poll_types.h"
#include "redact_pii.h"

#ifdef SHM_EXTRA_STATS
#include "mem/module_info.h"
Expand Down Expand Up @@ -394,6 +395,8 @@ extern int cfg_parse_only_routes;
%token TOS
%token DISABLE_DNS_FAILOVER
%token REDACT_PII_
%token REDACT_TEMPLATE
%token REDACT_MODE
%token DISABLE_DNS_BLACKLIST
%token DST_BLACKLIST
%token DISABLE_STATELESS_FWD
Expand Down Expand Up @@ -1592,7 +1595,53 @@ assign_stm: LOGLEVEL EQUAL snumber { IFOR();
| REDACT_PII_ EQUAL NUMBER { IFOR();
redact_pii_=$3;
}
| REDACT_PII_ error { yyerror("boolean value expected"); }
| REDACT_PII_ error { yyerror("boolean value expected"); }
| REDACT_TEMPLATE EQUAL STRING { IFOR();
redact_template=$3;
if (redact_mode == REDACT_FORMAT) {
char *pct = strstr(redact_template, "%s");
if (pct) {
redact_fmt.left.s = redact_template;
redact_fmt.left.len = pct - redact_template;
redact_fmt.right.s = pct + 2;
redact_fmt.right.len = strlen(pct + 2);
} else {
redact_fmt.left.s = redact_template;
redact_fmt.left.len = strlen(redact_template);
redact_fmt.right.s = "";
redact_fmt.right.len = 0;
}
}
}
| REDACT_TEMPLATE error { yyerror("string value expected"); }
| REDACT_MODE EQUAL STRING { IFOR();
if (strcasecmp($3, "replace")==0)
redact_mode=REDACT_REPLACE;
else if (strcasecmp($3, "append")==0)
redact_mode=REDACT_APPEND;
else if (strcasecmp($3, "prepend")==0)
redact_mode=REDACT_PREPEND;
else if (strcasecmp($3, "format")==0) {
redact_mode=REDACT_FORMAT;
if (redact_template) {
char *pct = strstr(redact_template, "%s");
if (pct) {
redact_fmt.left.s = redact_template;
redact_fmt.left.len = pct - redact_template;
redact_fmt.right.s = pct + 2;
redact_fmt.right.len = strlen(pct + 2);
} else {
redact_fmt.left.s = redact_template;
redact_fmt.left.len = strlen(redact_template);
redact_fmt.right.s = "";
redact_fmt.right.len = 0;
}
}
}
else
yyerror("redact_mode must be: replace|append|prepend|format");
}
| REDACT_MODE error { yyerror("string value expected (replace|append|prepend|format)"); }
| DISABLE_DNS_BLACKLIST EQUAL NUMBER { IFOR();
disable_dns_blacklist=$3;
}
Expand Down
54 changes: 52 additions & 2 deletions redact_pii.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,57 @@
#include "redact_pii.h"

int redact_pii_ = 0;
char *redact_template = "****";
int redact_mode = REDACT_REPLACE;
redact_log_format_t redact_fmt = {{NULL, 0}, {NULL, 0}};

inline const char* redact_pii(const char* input) {
return redact_pii_ ? "****" : ZSW(input);
#define REDACT_BUF_SIZE 512

inline const char* redact_pii(const char* input) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function needs proper bounds checking of the buffer in all cases except the replace case, we should create a reusable macro like so

#define SAFE_COPY_TO_BUF(_c, _len) \
    do { \
        size_t _n = (_len); \
        if (idx + _n < REDACT_BUF_SIZE) { \
            memcpy(buf + idx, (_c), _n); \
            idx += _n; \
        } else { \
            goto end; \
        } \
    } while (0)

An example with REDACT_APPEND using the macro

    case REDACT_APPEND:
        input_len = strnlen(safe, REDACT_BUF_SIZE);
        SAFE_COPY_TO_BUF(safe, input_len);
        SAFE_COPY_TO_BUF(redact_template.s, redact_template.len);
...
end;
	buf[idx] = '\0';
	return buf;

Also the redact_template should be constructed as a str not char* so that we have constant time access to the length rather than having to run strnlen everytime

One extra thing you'll notice is the use strnlen instead of strlen and it uses the REDACT_BUF_SIZE as the upper bounds guard, this will return 512 if there is null terminator \0 which will ensure we don't try to memcpy unsafely and it will end the copying at that point and null terminate the output buffer.

static char buf[REDACT_BUF_SIZE];
const char *safe = ZSW(input);
size_t input_len, idx;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initialize every member of the function at the start, the code in this comment relies on it

size_t input_len = 0, idx = 0;


if (!redact_pii_)
return safe;

switch (redact_mode) {
case REDACT_REPLACE:
return redact_template;
case REDACT_APPEND:
input_len = strlen(safe);
idx = 0;
memcpy(buf + idx, safe, input_len);
idx += input_len;
memcpy(buf + idx, redact_template, strlen(redact_template));
idx += strlen(redact_template);
buf[idx] = '\0';
return buf;
case REDACT_PREPEND:
input_len = strlen(safe);
idx = 0;
memcpy(buf + idx, redact_template, strlen(redact_template));
idx += strlen(redact_template);
memcpy(buf + idx, safe, input_len);
idx += input_len;
buf[idx] = '\0';
return buf;
case REDACT_FORMAT:
input_len = strlen(safe);
idx = 0;
if (redact_fmt.left.len > 0) {
memcpy(buf + idx, redact_fmt.left.s, redact_fmt.left.len);
idx += redact_fmt.left.len;
}
memcpy(buf + idx, safe, input_len);
idx += input_len;
if (redact_fmt.right.len > 0) {
memcpy(buf + idx, redact_fmt.right.s, redact_fmt.right.len);
idx += redact_fmt.right.len;
}
buf[idx] = '\0';
return buf;
default:
return redact_template;
}
}
19 changes: 19 additions & 0 deletions redact_pii.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@
#ifndef redact_pii_h
#define redact_pii_h

#include "str.h"

enum {
REDACT_REPLACE = 0,
REDACT_APPEND,
REDACT_PREPEND,
REDACT_FORMAT
};

typedef struct {
str left;
str right;
} redact_log_format_t;

extern int redact_pii_;
extern char *redact_template;
extern int redact_mode;
extern redact_log_format_t redact_fmt;

const char* redact_pii(const char* input);

#endif
Loading