Skip to content

Fix log injection attack and return on JSON parse failure #957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2018

Conversation

cdecker
Copy link
Member

@cdecker cdecker commented Feb 8, 2018

As reported by @practicalswift in #945 it is possible to inject
non-printable, or shell escape, characters in a json command, that
will fail to parse and then clear the shell.

The second commit also adds an error response when we're unable
to parse the request, just to be extra nice 😉

Fixes #945
Fixes #908

@cdecker cdecker self-assigned this Feb 8, 2018
@cdecker cdecker changed the title Issue 945 Fix log injection attack and return on JSON parse failure Feb 8, 2018
Copy link
Contributor

@rustyrussell rustyrussell left a comment

Choose a reason for hiding this comment

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

Let's catch this at the root: either in logv and logv_add (simple substitution of char < ' ' or == 127 with ? will do) or in the various print functions. That closes all future holes we might have.

@cdecker
Copy link
Member Author

cdecker commented Feb 9, 2018

I guess if we indeed have a shell escape character in a string, e.g., invoice description, that'll trigger an invalid token error? In that case, I'll move the check to logv and logv_add.

@cdecker
Copy link
Member Author

cdecker commented Feb 9, 2018

Removed check for unprintable characters in JSON, replaced with string sanitization in logv and logv_add. Please re-review @rustyrussell

Copy link
Contributor

@rustyrussell rustyrussell left a comment

Choose a reason for hiding this comment

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

Tiny suggestions, I'll leave to you to apply.

lightningd/log.c Outdated
@@ -289,6 +295,12 @@ void logv_add(struct log *log, const char *fmt, va_list ap)
list_del_from(&log->lr->log, &l->list);

tal_append_vfmt(&l->log, fmt, ap);

/* Sanitize any non-printable characters, and replace with '?' */
for (size_t i=0; i<strlen(l->log); i++)
Copy link
Contributor

Choose a reason for hiding this comment

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

You can iterate from oldlen, not 0, BTW, since this is a simple append...

lightningd/log.c Outdated

/* Sanitize any non-printable characters, and replace with '?' */
for (size_t i=0; i<strlen(l->log); i++)
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be checking == 0x7f so we let through UTF-8? That would seem sensible...

Copy link
Member Author

@cdecker cdecker Feb 10, 2018

Choose a reason for hiding this comment

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

As much as I'd like to have UTF-8 support in here, that'd mean we now need to carry context and have multi-char replacements in some cases, e.g., there are multi-byte control characters again.

As reported by @practicalswift in ElementsProject#945 it is possible to inject
non-printable, or shell escape, characters in a json command, that
will fail to parse and then clear the shell.

Reported-by: @practicalswift
Signed-off-by: Christian Decker <decker.christian@gmail.com>
The JSON-RPC spec specifies that if the request is unparseable we
should return an error with a NULL id. This is a bit more friendly
than slamming the door in the face.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
@cdecker
Copy link
Member Author

cdecker commented Feb 10, 2018

Rebased and started sanitization from oldlen as suggested by @rustyrussell

ACK 5267456

@cdecker cdecker merged commit 5a133d2 into ElementsProject:master Feb 10, 2018
@practicalswift
Copy link
Contributor

Post-merge ACK 5267456

Sorry for the late review :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants