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
384384 }
385385 EXPORT_SYMBOL(seq_escape);
386386
387+void 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+}
396+EXPORT_SYMBOL(seq_escape_mem_ascii);
397+
387398 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
388399 {
389400 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
127127 unsigned long long v, unsigned int width);
128128
129129 void seq_escape(struct seq_file *m, const char *s, const char *esc);
130+void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz);
130131
131132 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
132133 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
5454 int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
5555 unsigned int flags, const char *only);
5656
57+int string_escape_mem_ascii(const char *src, size_t isz, char *dst,
58+ size_t osz);
59+
5760 static inline int string_escape_mem_any_np(const char *src, size_t isz,
5861 char *dst, size_t osz, const char *only)
5962 {
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
540540 }
541541 EXPORT_SYMBOL(string_escape_mem);
542542
543+int 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+}
560+EXPORT_SYMBOL(string_escape_mem_ascii);
561+
543562 /*
544563 * Return an allocated string that has been escaped of special characters
545564 * 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