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

cmd/snap-confine: nvidia: preserve globbed file prefix #4902

Merged
merged 1 commit into from Mar 23, 2018

Conversation

bboozzoo
Copy link
Collaborator

When symlinking libgl files from hostfs we discard the path prefix inside
source_dir of a globbed file and overwrite already symlinked files if those are
created before.

For example, nvidia globs:

  • libnvidia-tls.so*
  • tls/libnvidia-tls.so*

Produce the following matches:

  • ${source_dir}/libnvidia-tls.so
  • ${source_dir}/libnvidia-tls.so.390.42
  • ${source_dir}/tls/libnvidia-tls.so
  • ${source_dir}/tls/libnvidia-tls.so.390.42

When the files under ${source_dir}/tls are processed, eg.
${source_dir}/tls/libnvidia-tls.so.390.42, the symlink we create will
overwrite ${libgl_dir}/libnvidia-tls.so.390.42, thus shadowing the original
library that was present at this path.

The patch attempts to fix this behavior and mirrors the source_dir globs, thus
the following symlinks are created:

  • ${libgl_dir}/libnvidia-tls.so.390.42 -> ${source_dir}/libnvidia-tls.so.390.42
  • ${libgl_dir}/tls/libnvidia-tls.so.390.42 -> ${source_dir}/tsls/libnvidia-tls.so.390.42

@bboozzoo bboozzoo added this to the 2.32 milestone Mar 22, 2018
@codecov-io
Copy link

codecov-io commented Mar 22, 2018

Codecov Report

Merging #4902 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #4902   +/-   ##
=======================================
  Coverage   78.96%   78.96%           
=======================================
  Files         478      478           
  Lines       34902    34902           
=======================================
  Hits        27559    27559           
  Misses       5152     5152           
  Partials     2191     2191

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9ea2acd...924719e. Read the comment docs.

Copy link
Collaborator

@zyga zyga left a comment

Choose a reason for hiding this comment

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

Some comments inline. I'm marking this as request changes because of missing NULL pointer checks.

const char *pathname = glob_res.gl_pathv[i];
char *pathname_copy
SC_CLEANUP(sc_cleanup_string) = strdup(pathname);
char *dirname_copy
SC_CLEANUP(sc_cleanup_string) = strdup(pathname);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please check for both of those being NULL.

char *filename = basename(pathname_copy);
char *directory_name = dirname(dirname_copy);
strncpy(prefix_dir, libgl_dir, sizeof prefix_dir);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please use sc_must_snprintf instead, it's a bit easier to get right than knowing exactly how strncpy fails.

char *directory_name = dirname(dirname_copy);
strncpy(prefix_dir, libgl_dir, sizeof prefix_dir);

if (strlen(directory_name) > source_dir_len) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, can you add a comment what is going on here? I'm a bit slow today.

Copy link

@jdstrand jdstrand left a comment

Choose a reason for hiding this comment

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

I've not reviewed the correctness of the code, just the safety of it. Please perform @zyga's recommendations, then +1.

@@ -123,6 +125,7 @@ static void sc_populate_libgl_with_hostfs_symlinks(const char *libgl_dir,
const char *glob_list[],
size_t glob_list_len)
{
size_t source_dir_len = strlen(source_dir);

Choose a reason for hiding this comment

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

strlen() can be tricky with untrusted inputs, but I verified the callers of this function and all of the inputs are trusted.

@bboozzoo
Copy link
Collaborator Author

I'll amend & force push if you don't mind. This will make cherry-picking easier in case we need it.

@bboozzoo bboozzoo force-pushed the bboozzoo/nvidia-preserve-glob-prefix branch 2 times, most recently from 430da86 to 9c61fb7 Compare March 23, 2018 08:08
const char *pathname = glob_res.gl_pathv[i];
char *pathname_copy
SC_CLEANUP(sc_cleanup_string) = strdup(pathname);
char *dirname_copy
SC_CLEANUP(sc_cleanup_string) = strdup(pathname);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you mean to strdup pathname again? It looks like a bug

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes I totally mean it. POSIX dirname() modifies its input argument.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you call those pathname_copy1 and pathname_copy2 and add a comment describing why we need them. A naive quick look (I agree this is right but just confusing) may mistake this for copy-paste error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Force pushed

When symlinking libgl files from hostfs we discard the path prefix inside
source_dir of a globbed file and overwrite already symlinked files if those are
created before.

For example, nvidia globs:
  libnvidia-tls.so*
  tls/libnvidia-tls.so*

Produce the following matches:

  ${source_dir}/libnvidia-tls.so
  ${source_dir}/libnvidia-tls.so.390.42
  ${source_dir}/tls/libnvidia-tls.so
  ${source_dir}/tls/libnvidia-tls.so.390.42

When the files under ${source_dir}/tls are processed, eg.
${source_dir}/tls/libnvidia-tls.so.390.42 the symlink we create will
overwrite ${libgl_dir}/libnvidia-tls.so.390.42, thus shadowing the original
library that was present at this path.

The patch attempts to fix this behavior and mirrors the source_dir globs, thus
the following symlinks are created:

  ${libgl_dir}/libnvidia-tls.so.390.42 -> ${source_dir}/libnvidia-tls.so.390.42
  ${libgl_dir}/tls/libnvidia-tls.so.390.42 -> ${source_dir}/tsls/libnvidia-tls.so.390.42

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
@bboozzoo bboozzoo force-pushed the bboozzoo/nvidia-preserve-glob-prefix branch from 9c61fb7 to 924719e Compare March 23, 2018 09:05
@zyga zyga requested a review from mvo5 March 23, 2018 16:17
@mvo5 mvo5 merged commit cdb476c into snapcore:master Mar 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants