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 parseInfoFile does not handle spaces in filenames #38977

Merged
merged 1 commit into from
Apr 2, 2019

Conversation

thaJeztah
Copy link
Member

fixes #38458
addresses kubernetes/kubernetes#35062

/proc/self/mountinfo uses \040 for spaces, however, parseInfoFile()
did not decode those spaces in paths, therefore attempting to use \040
as a literal part of the path.

This patch un-quotes the root and mount point fields to fix
situations where paths contain spaces.

Note that the mount source field is not modified, given that
this field is documented (man PROC(5)) as:

filesystem-specific information or "none"

Which I interpreted as "the format in this field is undefined".

Thanks to @remexre for reporting, and @itizir, @sergei-utinski for suggesting the patch.

@thaJeztah
Copy link
Member Author

ping @kolyshkin @cpuguy83 PTAL

@thaJeztah thaJeztah force-pushed the fix_parseinfofile_parsing branch 2 times, most recently from c668e38 to d17e07a Compare March 29, 2019 15:58
@thaJeztah thaJeztah requested a review from kolyshkin April 1, 2019 14:30
@@ -52,8 +56,17 @@ func parseInfoFile(r io.Reader, filter FilterFunc) ([]*Info, error) {
p.Major, _ = strconv.Atoi(mm[0])
p.Minor, _ = strconv.Atoi(mm[1])

p.Root = fields[3]
p.Mountpoint = fields[4]
r, err := strconv.Unquote(`"` + fields[3] + `"`)
Copy link
Contributor

Choose a reason for hiding this comment

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

According to kernel sources, the only characters replaced by \nnn are space, tab, newline, and backslash (\).

Initially I was opposed to using strconv.Unquote() but it seems if \ is always escaped, Unquote can do no harm.

BTW, note that Linux file paths can indeed contain newlines, tabs, and backslashes :=0

}
p.Root = r

mp, err := strconv.Unquote(`"` + fields[4] + `"`)
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no need to have a temp variable here, since you exit on error anyway, so maybe

root.Mountpoint, err := strconv.Unquote(`"` + fields[4] + `"`)
if err != nil {
 			return nil, errors.Wrapf(err, "Can't parse mount point %q", fields[4])
}

(I have also made error message smaller, but it's up to you)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I wanted to do a follow up to fix the error messages (also make them lowercase); for this fix, I went for consistency with the existing ones.

Copy link
Contributor

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

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

Added a few comments, but it's all very minor nitpicks so LGTM

@kolyshkin
Copy link
Contributor

BTW this needs to be ported to containerd, too.

(as a side note, I am puzzled why I haven't fixed this as part of https://github.com/moby/moby/pull/36091as I was very much aware of the issue)

@codecov
Copy link

codecov bot commented Apr 2, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@32157f9). Click here to learn what that means.
The diff coverage is 37.5%.

@@            Coverage Diff            @@
##             master   #38977   +/-   ##
=========================================
  Coverage          ?   36.89%           
=========================================
  Files             ?      614           
  Lines             ?    45414           
  Branches          ?        0           
=========================================
  Hits              ?    16756           
  Misses            ?    26365           
  Partials          ?     2293

@thaJeztah
Copy link
Member Author

Updated; removed the intermediate variable (no other changes)

`/proc/self/mountinfo` uses `\040` for spaces, however, `parseInfoFile()`
did not decode those spaces in paths, therefore attempting to use `\040`
as a literal part of the path.

This patch un-quotes the `root` and `mount point` fields to fix
situations where paths contain spaces.

Note that the `mount source` field is not modified, given that
this field is documented (man `PROC(5)`) as:

    filesystem-specific information or "none"

Which I interpreted as "the format in this field is undefined".

Reported-by: Daniil Yaroslavtsev <daniilyar@users.noreply.github.com>
Reported-by: Nathan Ringo <remexre@gmail.com>
Based-on-patch-by: Diego Becciolini <itizir@users.noreply.github.com>
Based-on-patch-by: Sergei Utinski <sergei-utinski@users.noreply.github.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@kolyshkin kolyshkin merged commit 0133041 into moby:master Apr 2, 2019
@thaJeztah thaJeztah deleted the fix_parseinfofile_parsing branch April 2, 2019 16:32
kolyshkin added a commit to kolyshkin/sys that referenced this pull request Jun 23, 2020
Moby PR #38977 [1] and containerd PR #3159 [2] added the handing of
escape sequences in mountinfo paths (root and mountpoint fields).

Unfortunately, it also broke the handling of paths containing double
quotes, as it was pointed out in [3].

The solution is to stop using strconv.Unquote and write our own
specialized function to deal with escape sequences.

Unit tests added.

[1] moby/moby#38977
[2] containerd/containerd#3159
[3] containerd/containerd#4257

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin added a commit to kolyshkin/sys that referenced this pull request Jun 23, 2020
Moby PR #38977 [1] and containerd PR #3159 [2] added the handing of
escape sequences in mountinfo paths (root and mountpoint fields).

Unfortunately, it also broke the handling of paths containing double
quotes, as it was pointed out in [3].

The solution is to stop using strconv.Unquote and write our own
specialized function to deal with escape sequences.

Unit tests added.

[1] moby/moby#38977
[2] containerd/containerd#3159
[3] containerd/containerd#4257

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin added a commit to kolyshkin/sys that referenced this pull request Jun 23, 2020
Moby PR #38977 [1] and containerd PR #3159 [2] added the handing of
escape sequences in mountinfo paths (root and mountpoint fields).

Unfortunately, it also broke the handling of paths containing double
quotes, as it was pointed out in [3].

The solution is to stop using strconv.Unquote and write our own
specialized function to deal with escape sequences.

Unit tests added.

[1] moby/moby#38977
[2] containerd/containerd#3159
[3] containerd/containerd#4257

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
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.

parseInfoFile does not handle spaces in filenames
3 participants