History

nfsd: escape high characters in binary data

I'm exposing some information about NFS clients in pseudofiles. I expect to eventually have simple tools to help read those pseudofiles. But it's also helpful if the raw files are human-readable to the extent possible. It aids debugging and makes them usable on systems that don't have the latest nfs-utils. A minor challenge there is opaque client-generated protocol objects like state owners and client identifiers. Some clients generate those to include handy information in plain ascii. But they may also include arbitrary byte sequences. I think the simplest approach is to limit to isprint(c) && isascii(c) and escape everything else. That means you can just cat the file and get something that looks OK. Also, I'm trying to keep these files legal YAML, which requires them to UTF-8, and this is a simple way to guarantee that.

J. Bruce Fields committed 7 years ago

Commit ea053e11 parent 3bade244 files · +34 −0Browse files

Unified · Split

4 files
  1. fs/seq_file.c +11 −0
  2. include/linux/seq_file.h +1 −0
  3. include/linux/string_helpers.h +3 −0
  4. lib/string_helpers.c +19 −0
fs/seq_file.c+11−0
@@ -384,6 +384,17 @@ void seq_escape(struct seq_file *m, const char *s, const char *esc) · 383 unmodified lines
384}384}
385EXPORT_SYMBOL(seq_escape);385EXPORT_SYMBOL(seq_escape);
386 386
387void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz)
388{
389 char *buf;
390 size_t size = seq_get_buf(m, &buf);
391 int ret;
392
393 ret = string_escape_mem_ascii(src, isz, buf, size);
394 seq_commit(m, ret < size ? ret : -1);
395}
396EXPORT_SYMBOL(seq_escape_mem_ascii);
397
387void seq_vprintf(struct seq_file *m, const char *f, va_list args)398void seq_vprintf(struct seq_file *m, const char *f, va_list args)
388{399{
389 int len;400 int len;
include/linux/seq_file.h+1−0
@@ -127,6 +127,7 @@ void seq_put_hex_ll(struct seq_file *m, const char *delimiter, · 126 unmodified lines
127 unsigned long long v, unsigned int width);127 unsigned long long v, unsigned int width);
128 128
129void seq_escape(struct seq_file *m, const char *s, const char *esc);129void seq_escape(struct seq_file *m, const char *s, const char *esc);
130void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz);
130 131
131void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,132void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
132 int rowsize, int groupsize, const void *buf, size_t len,133 int rowsize, int groupsize, const void *buf, size_t len,
include/linux/string_helpers.h+3−0
@@ -54,6 +54,9 @@ static inline int string_unescape_any_inplace(char *buf) · 53 unmodified lines
54int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,54int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
55 unsigned int flags, const char *only);55 unsigned int flags, const char *only);
56 56
57int string_escape_mem_ascii(const char *src, size_t isz, char *dst,
58 size_t osz);
59
57static inline int string_escape_mem_any_np(const char *src, size_t isz,60static inline int string_escape_mem_any_np(const char *src, size_t isz,
58 char *dst, size_t osz, const char *only)61 char *dst, size_t osz, const char *only)
59{62{
lib/string_helpers.c+19−0
@@ -540,6 +540,25 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, · 539 unmodified lines
540}540}
541EXPORT_SYMBOL(string_escape_mem);541EXPORT_SYMBOL(string_escape_mem);
542 542
543int string_escape_mem_ascii(const char *src, size_t isz, char *dst,
544 size_t osz)
545{
546 char *p = dst;
547 char *end = p + osz;
548
549 while (isz--) {
550 unsigned char c = *src++;
551
552 if (!isprint(c) || !isascii(c) || c == '"' || c == '\\')
553 escape_hex(c, &p, end);
554 else
555 escape_passthrough(c, &p, end);
556 }
557
558 return p - dst;
559}
560EXPORT_SYMBOL(string_escape_mem_ascii);
561
543/*562/*
544 * Return an allocated string that has been escaped of special characters563 * Return an allocated string that has been escaped of special characters
545 * and double quotes, making it safe to log in quotes.564 * and double quotes, making it safe to log in quotes.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t