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

osutil: fix parsing super-block options with spaces #12899

Merged
merged 4 commits into from
Aug 3, 2023

Conversation

zyga
Copy link
Contributor

@zyga zyga commented Jun 17, 2023

On any Windows + WSL 2 + Docker system, running any snap command is immediately preceded by the two errors:

  2023/06/17 23:59:52.071511 system_key.go:129: cannot determine nfs usage in
  generateSystemKey: cannot parse mountinfo: incorrect number of tail fields,
  expected 3 but found 4
  2023/06/17 23:59:52.080033 cmd_run.go:1046: WARNING: cannot create user data
  directory: cannot determine SELinux status: failed to obtain SELinux mount
  path: incorrect number of tail fields, expected 3 but found 4

Those errors confuse tools that parse program output and generally cause a lot of havoc.

The root of the issue is caused by this specific entry in /proc/self/mountinfo (not wrapped to preserve the problematic part more prominently):

  1146 77 0:149 / /Docker/host rw,noatime - 9p drvfs rw,dirsync,aname=drvfs;path=C:\Program Files\Docker\Docker\resources;symlinkroot=/mnt/,mmap,access=client,msize=262144,trans=virtio

Note that the super-block mount option contains a key=value pair where the value contains un-escaped spaces. Historically mountinfo has been an utter mess to parse, with several bugs in both the userspace parsing and several bugs in the fragile kernel interface generating the contents of said file.

To work around this problem allow spaces in the super-block options and silently parse them as if they had been escaped correctly.

On any Windows + WSL 2 + Docker system, running any snap command is immediately
preceded by the two errors:

  2023/06/17 23:59:52.071511 system_key.go:129: cannot determine nfs usage in
  generateSystemKey: cannot parse mountinfo: incorrect number of tail fields,
  expected 3 but found 4
  2023/06/17 23:59:52.080033 cmd_run.go:1046: WARNING: cannot create user data
  directory: cannot determine SELinux status: failed to obtain SELinux mount
  path: incorrect number of tail fields, expected 3 but found 4

Those errors confuse tools that parse program output and generally cause a lot of havoc.

The root of the issue is caused by this specific entry in /proc/self/mountinfo
(not wrapped to preserve the problematic part more prominently):

  1146 77 0:149 / /Docker/host rw,noatime - 9p drvfs rw,dirsync,aname=drvfs;path=C:\Program Files\Docker\Docker\resources;symlinkroot=/mnt/,mmap,access=client,msize=262144,trans=virtio

Note that the super-block mount option contains a key=value pair where the
value contains un-escaped spaces. Historically mountinfo has been an utter mess
to parse, with several bugs in both the userspace parsing and several bugs in
the fragile kernel interface generating the contents of said file.

To work around this problem allow spaces in the super-block options and
silently parse them as if they had been escaped correctly.

Signed-off-by: Zygmunt Krynicki <[email protected]>
@codecov-commenter
Copy link

codecov-commenter commented Jun 17, 2023

Codecov Report

Merging #12899 (0ecf780) into master (03a3c48) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@           Coverage Diff           @@
##           master   #12899   +/-   ##
=======================================
  Coverage   78.59%   78.60%           
=======================================
  Files         992      992           
  Lines      123110   123111    +1     
=======================================
+ Hits        96760    96767    +7     
+ Misses      20247    20244    -3     
+ Partials     6103     6100    -3     
Flag Coverage Δ
unittests 78.60% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
osutil/mountinfo_linux.go 94.77% <100.00%> (+0.03%) ⬆️

... and 4 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

On any Windows + WSL 2 + Docker system, running any snap command is immediately
preceded by the two errors:

  2023/06/17 23:59:52.071511 system_key.go:129: cannot determine nfs usage in
  generateSystemKey: cannot parse mountinfo: incorrect number of tail fields,
  expected 3 but found 4
  2023/06/17 23:59:52.080033 cmd_run.go:1046: WARNING: cannot create user data
  directory: cannot determine SELinux status: failed to obtain SELinux mount
  path: incorrect number of tail fields, expected 3 but found 4

Those errors confuse tools that parse program output and generally cause a lot of havoc.

The root of the issue is caused by this specific entry in /proc/self/mountinfo
(not wrapped to preserve the problematic part more prominently):

  1146 77 0:149 / /Docker/host rw,noatime - 9p drvfs rw,dirsync,aname=drvfs;path=C:\Program Files\Docker\Docker\resources;symlinkroot=/mnt/,mmap,access=client,msize=262144,trans=virtio

Note that the super-block mount option contains a key=value pair where the
value contains un-escaped spaces. Historically mountinfo has been an utter mess
to parse, with several bugs in both the userspace parsing and several bugs in
the fragile kernel interface generating the contents of said file.

To work around this problem allow spaces in the super-block options and
silently parse them as if they had been escaped correctly.

Signed-off-by: Zygmunt Krynicki <[email protected]>
@zyga zyga requested a review from alexmurray June 17, 2023 22:32
@mvo5 mvo5 added the Needs security review Can only be merged once security gave a :+1: label Jun 19, 2023
@zyga
Copy link
Contributor Author

zyga commented Jul 11, 2023

Hey @alexmurray - this is a small patch that makes snaps usable on WSL if you also happen to have Docker installed - please tell me if the way the change is made is sensible, or of some other approach is viable in Your eyes. Thanks!

@alexmurray
Copy link
Contributor

hey @zyga - apologies I'm taking to long to get to this - I hope to take a proper look tomorrow so with any luck it should be done on my side in 24h

Copy link
Contributor

@alexmurray alexmurray left a comment

Choose a reason for hiding this comment

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

LGTM - whilst this change adds some complexity and makes this already fragile code a bit more fragile, I think it is definitely worth it to support this reasonably common use-case. The nature of /proc/mountinfo lacking good uniform escaping etc means we have to try and be more lenient. Also this is already a trusted input so it doesn't add any extra risk to add this complexity.

Thanks @zyga.

@alexmurray alexmurray removed the Needs security review Can only be merged once security gave a :+1: label Jul 13, 2023
@zyga
Copy link
Contributor Author

zyga commented Jul 17, 2023

@mvo5 could this be considered for the next release?

@pedronis pedronis added this to the 2.61 milestone Jul 28, 2023
* rename "last"->"allow_spaces_in_field" in parse_next_string_field_ex()
* add comments to `parse_{next,last}_string_field()`
Copy link
Contributor

@mvo5 mvo5 left a comment

Choose a reason for hiding this comment

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

Thanks! I like this! I tweaked the naming and added a small comment. But did not change anything else.

@mvo5 mvo5 merged commit 868459b into canonical:master Aug 3, 2023
@zyga zyga deleted the fix/superblock-spaces branch August 22, 2023 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants