Skip to content
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

Fix crash when an element of SymbolsBody is empty #423

Merged
merged 1 commit into from
Feb 5, 2024

Conversation

chiro
Copy link
Contributor

@chiro chiro commented Dec 27, 2023

An empty element is allowed in SymbolsBody definition, so the following keymap is gramatically correct.

xkb_keymap {
  xkb_symbols "sym" {
    key <SPC> {, [Space] };
  };
};

However, the current parser crashes with the keymap due to null pointer access.
This change fixes the crash.

@wismill
Copy link
Member

wismill commented Dec 27, 2023

Thank you for the bug report.

I confirm this syntax is accepted by xkbcomp and is interpreted as if there was no leading comma.

But I think this is an oversight, because this syntax is useless. As Ivan Pascal wrote, the intended syntax would rather be:

key <SPC> {[], [Space] };

which parses correctly in xkbcommon.

Anyway, a segmentation fault is definitly not acceptable!

@wismill wismill added bug Indicates an unexpected problem or unintended behavior compile-keymap Indicates a need for improvements or additions to keymap compilation labels Dec 27, 2023
@wismill wismill added this to the 1.7.0 milestone Dec 27, 2023
Copy link
Member

@wismill wismill left a comment

Choose a reason for hiding this comment

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

@chiro The fix LGTM, but the test needs some fixes, please look at my suggestions.

@whot @bluetech this PR would allow to parse input currently accepted by xkbcomp, but I think it really should not be accepted there either.

@chiro
Copy link
Contributor Author

chiro commented Dec 27, 2023

@wismill Thanks for the prompt review! I fixed the test. PTAL again.

I agree that the parser rule is not useful. However changing it introduces backward incompatibility, so I decided to fix the crash for now.

Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

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

Thanks @chiro, LGTM.

Agree it's not worth breaking backward compat over.

I'll leave it to @wismill to make the final decision.

xkb_types { include "complete" };
xkb_compat { include "complete" };
xkb_symbols "sym" {
key <SPC> { , [Space] };
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
key <SPC> { , [Space] };
// This dubious syntax is accepted by xkbcomp so we accept it for
// backward compatibility. The preferred way to write it is:
// key <SPC> { [], [Space] };
key <SPC> { , [Space] };

Copy link
Member

Choose a reason for hiding this comment

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

Actually, these syntaxes are not equivalent. Using X11 xkbcomp, I get that

key <SPC> { [Space] };
key <SPC> { , [exclam] };

is equivalent to

key <SPC> { [exclam] };

whereas

key <SPC> { [Space] };
key <SPC> { [], [exclam] };

is equivalent to

key  <SPC> {
    symbols[Group1]= [          Space ],
    symbols[Group2]= [          exclam ]
};

That’s why I wrote it was probably an oversight.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the clarification. then, is there any other changes needed?

Copy link
Member

Choose a reason for hiding this comment

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

@chiro I wonder: where did you encounter this syntax in the first place? Unless there is a real-world proof, I think we should fix just the segfault, but disallow this syntax. It is useless per se and confusing. It is not used in xkeyboard-config.

I do not see to make key <SPC> { , [exclam] }; equivalent to key <SPC> { [], [exclam] }; either: XKB has C-style, so it would not be intuitive.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fully agree with @wismill here - if this is somewhere in the xkb maps we should fix it in xkeyboard-config but allowing this syntax seems like the wrong approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wismill It was not found in an actual in-use layout file. It's detected in testing. As far as I know, there is no real-world use case of this syntax. I have no objection to disallowing this syntax.

Just to clarify, I couldn't find any real-world use case, but it doesn't mean there is no use case in the world. That's why I made the CL just to fix the crash.

Copy link
Member

Choose a reason for hiding this comment

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

@chiro we are grateful for uncovering the segfault. This is a nasty bug, and it would not occur to me to test this syntax. Well done!

I am curious, could you share how you stumble on this? Maybe we could then improve our test suite further.

Now, given that:

  • AFAIK the syntax is useless and undocumented;
  • xkeyboard-config compiles just fine with xkbcommon;
  • no one opened a ticket before.

then I really doubt there are use cases in the world. While we strive to have maximum compatibility with X11, I think this is one edge case we should not support. Probably worth a comment, at least in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was reported as a potential security issue to the project I'm working on (Actually it was not because only layout files in xkeyboard-config are used). I don't know the details of how it was discovered, but it should be by fuzzing.

Copy link
Contributor

Choose a reason for hiding this comment

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

If it's not used in any existing xkeyboard-config files, can we just make it return a parser error? That seems like the best option here - making this intentionally backwards-compatible means we have to support it forever when there seems to be little point to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comment (and the reaction)! I changed this PR not to allow the syntax. Could you review it again? Thanks.

xkb_types { include "complete" };
xkb_compat { include "complete" };
xkb_symbols "sym" {
key <SPC> { , [Space] };
Copy link
Member

Choose a reason for hiding this comment

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

@chiro I wonder: where did you encounter this syntax in the first place? Unless there is a real-world proof, I think we should fix just the segfault, but disallow this syntax. It is useless per se and confusing. It is not used in xkeyboard-config.

I do not see to make key <SPC> { , [exclam] }; equivalent to key <SPC> { [], [exclam] }; either: XKB has C-style, so it would not be intuitive.

An empty element is allowed in SymbolsBody definition, so the following
keymap is gramatically correct.

```
xkb_keymap {
  ...
  xkb_symbols "sym" {
    key <SPC> {, [Space] };
  };
};
```

However, the current parser crashes with the keymap due to null pointer
access.
This change fixes it by changing the parser not to allow it.
Copy link
Contributor

@whot whot left a comment

Choose a reason for hiding this comment

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

LGTM now, @wismill can you merge it if you're happy with it? Thanks.

@wismill wismill merged commit efdb05d into xkbcommon:master Feb 5, 2024
4 checks passed
@wismill
Copy link
Member

wismill commented Feb 5, 2024

Thanks @chiro !

@chiro chiro deleted the fix_crash branch February 6, 2024 01:27
@chiro
Copy link
Contributor Author

chiro commented Feb 6, 2024

Thank you all for reviewing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior compile-keymap Indicates a need for improvements or additions to keymap compilation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants