Skip to contentSkip to conversation

should GNU install call matchpathcon by default?

DISCUSSION Jim Meyering 4 participants 11 messages Nov 7, 2007 – May 21, 2008 1 message not in the archive SELinux security module

lore
Share & tools
lore
#0 lore raw

Hi,

This morning I noticed a flagrant difference in the speed of "make install" for the just-released gettext-0.17. It took 12(!) times longer on a rawhide system than on a usually-slower debian unstable system. (3min vs. 15s)

I tracked it down to the fact that on that rawhide system, I'm using the latest _upstream_ install binary from (SELinux-enabled) coreutils. The rawhide /usr/bin/install only ever calls matchpathcon (via setdefaultfilecon) when its final chown or chmod fails. Looks like a fortuitous bug in the rawhide patch. The version of install from upstream coreutils calls it every time, since that's the only thing that made sense to me when I redid coreutils' SELinux support. Considering no one can be relying on install's matchpathcon semantics, even in rawhide, I think it should not be the default, upstream -- especially considering the enormous performance hit.

Any objections or suggestions from the SELinux folks?

Do you think coreutils' install should use matchpathcon at all? If so, either it'll have to be associated with a new option, or it'll have to be a lot more efficient. Yes, we've discussed this before, and you guys convinced me to provide the functionality for ease of use. But for those not familiar with the issues, note that you can get the same effect without the embedded matchpathcon call, simply by invoking install with a context provided by the matchpathcon _program_:

  install -Z $(matchpathcon $dest_file) SRC_FILE $dest_file

Can someone can look at making matchpathcon more efficient? I understand that making it the default would be nice, and if it's feasible to minimize the performance impact of that matchpathcon call, it'd be much better *not* to add a new option.

Unless I find a magic bullet, I'll disable install's use of matchpathcon for the upcoming test release.

Jim

In case you're interested in trying a recent snapshot,

http://meyering.net/cu/coreutils-6.9-ss.tar.gz
http://meyering.net/cu/coreutils-6.9-ss.tar.gz.sig

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#1 lore raw
Quoted 4 lines · Jim Meyering

This morning I noticed a flagrant difference in the speed of "make install" for the just-released gettext-0.17. It took 12(!) times longer on a rawhide system than on a usually-slower debian unstable system. (3min vs. 15s)

FYI,

Dan Walsh suggested to use
matchpathcon_init_prefix (NULL, "/first_component_of_abs_dest/"); to limit the number of regular expressions matchpathcon will have to compile. That works very well, as long as you're not installing into /usr, in which case it's still better than nothing. When installing into /tmp, the example above takes 21-22 seconds, rather than 180. Much better. However, installing into /usr/tmp still required about 70 seconds, so there's room for improvement.

I've implemented that, but haven't finished testing.

For reference, the gettext installation process invokes
install -c -m ... src_file dst_file
1278 times.

Another suggestion:

- avoid using matchpathcon altogether when there's an existing destination file: use its context, instead.

I'm not convinced that this is necessary.
Other opinions?

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#2 lore raw
Quoted 8 lines · Jim Meyering
Quoted 4 lines · Jim Meyering

This morning I noticed a flagrant difference in the speed of "make install" for the just-released gettext-0.17. It took 12(!) times longer on a rawhide system than on a usually-slower debian unstable system. (3min vs. 15s)

FYI,

Quoted 8 lines

Dan Walsh suggested to use
matchpathcon_init_prefix (NULL, "/first_component_of_abs_dest/"); to limit the number of regular expressions matchpathcon will have to compile. That works very well, as long as you're not installing into /usr, in which case it's still better than nothing. When installing into /tmp, the example above takes 21-22 seconds, rather than 180. Much better. However, installing into /usr/tmp still required about 70 seconds, so there's room for improvement.

I've implemented it like this:

install+SELinux: reduce a 12x performance hit to ~1.5x
* src/install.c (setdefaultfilecon): Call matchpathcon_init_prefix, to mitigate what would otherwise be a large performance hit due to the use of matchpathcon.
Dan Walsh suggested the use of matchpathcon_init_prefix.
* gl/lib/se-selinux.in.h (matchpathcon_init_prefix): Define.

---
 gl/lib/se-selinux.in.h |    3 +++
 src/install.c          |   32 ++++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 0 deletions(-)
gl/lib/se-selinux.in.h +3 -0
index 7bfe4c5..7be1e70 100644
@@ -51,4 +51,7 @@ static inline int security_compute_create (security_context_t scon,
 					   security_class_t tclass,
 					   security_context_t *newcon)
   { errno = ENOTSUP; return -1; }
+static inline int matchpathcon_init_prefix (char const *path,
+					    char const *prefix)
+  { errno = ENOTSUP; return -1; }
 #endif
src/install.c +32 -0
index 34f61ff..216715f 100644
@@ -213,6 +213,38 @@ setdefaultfilecon (char const *file)
   if (lstat (file, &st) != 0)
     return;
 
+  if (IS_ABSOLUTE_FILE_NAME (file))
+    {
+      /* Calling matchpathcon_init_prefix (NULL, "/first_component/")
+	 is an optimization to minimize the expense of the following
+	 matchpathcon call.  */
+      char const *p0;
+      char const *p = file + 1;
+      while (ISSLASH (*p))
+	++p;
+
+      /* Record final leading slash, for when FILE starts with two or more.  */
+      p0 = p - 1;
+
+      if (*p)
+	{
+	  char *prefix;
+	  do
+	    {
+	      ++p;
+	    }
+	  while (*p && !ISSLASH (*p));
+
+	  prefix = malloc (p - p0 + 2);
+	  if (prefix)
+	    {
+	      stpcpy (stpncpy (prefix, p0, p - p0), "/");
+	      matchpathcon_init_prefix (NULL, prefix);
+	      free (prefix);
+	    }
+	}
+    }
+
   /* If there's an error determining the context, or it has none,
      return to allow default context */
   if ((matchpathcon (file, st.st_mode, &scontext) != 0) ||

--
1.5.3.5.643.g40e25

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#3 lore raw
Quoted 77 lines · Jim Meyering
Quoted 8 lines · Jim Meyering
Quoted 4 lines · Jim Meyering

This morning I noticed a flagrant difference in the speed of "make install" for the just-released gettext-0.17. It took 12(!) times longer on a rawhide system than on a usually-slower debian unstable system. (3min vs. 15s)

FYI,

Quoted 8 lines

Dan Walsh suggested to use
matchpathcon_init_prefix (NULL, "/first_component_of_abs_dest/"); to limit the number of regular expressions matchpathcon will have to compile. That works very well, as long as you're not installing into /usr, in which case it's still better than nothing. When installing into /tmp, the example above takes 21-22 seconds, rather than 180. Much better. However, installing into /usr/tmp still required about 70 seconds, so there's room for improvement.

I've implemented it like this:

install+SELinux: reduce a 12x performance hit to ~1.5x
* src/install.c (setdefaultfilecon): Call matchpathcon_init_prefix, to mitigate what would otherwise be a large performance hit due to the use of matchpathcon.
Dan Walsh suggested the use of matchpathcon_init_prefix.
* gl/lib/se-selinux.in.h (matchpathcon_init_prefix): Define.

---
 gl/lib/se-selinux.in.h |    3 +++
 src/install.c          |   32 ++++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 0 deletions(-)
gl/lib/se-selinux.in.h +3 -0
index 7bfe4c5..7be1e70 100644
@@ -51,4 +51,7 @@ static inline int security_compute_create (security_context_t scon,
 					   security_class_t tclass,
 					   security_context_t *newcon)
   { errno = ENOTSUP; return -1; }
+static inline int matchpathcon_init_prefix (char const *path,
+					    char const *prefix)
+  { errno = ENOTSUP; return -1; }
 #endif
src/install.c +25 -0
index 34f61ff..216715f 100644
@@ -213,6 +213,38 @@ setdefaultfilecon (char const *file)
   if (lstat (file, &st) != 0)
     return;
 
+  if (IS_ABSOLUTE_FILE_NAME (file))
+    {
+      /* Calling matchpathcon_init_prefix (NULL, "/first_component/")
+	 is an optimization to minimize the expense of the following
+	 matchpathcon call.  */
+      char const *p0;
+      char const *p = file + 1;
+      while (ISSLASH (*p))
+	++p;
+
+      /* Record final leading slash, for when FILE starts with two or more.  */
+      p0 = p - 1;
+
+      if (*p)
+	{
+	  char *prefix;
+	  do
+	    {
+	      ++p;
+	    }
+	  while (*p && !ISSLASH (*p));
+
+	  prefix = malloc (p - p0 + 2);
+	  if (prefix)
+	    {

src/install.c setdefaultfilecon (char const *file) · line 250

Quoted 10 lines
src/install.c +7 -0
+	      stpcpy (stpncpy (prefix, p0, p - p0), "/");
+	      matchpathcon_init_prefix (NULL, prefix);
+	      free (prefix);
+	    }
+	}
+    }
+
   /* If there's an error determining the context, or it has none,
      return to allow default context */
   if ((matchpathcon (file, st.st_mode, &scontext) != 0) ||

This issue came up recently again, see:
https://bugzilla.redhat.com/show_bug.cgi?id=447410

It appears that the patch that was merged into coreutils ends up calling matchpathcon_init_prefix() for each file being installed rather than once upon startup, and without calling matchpathcon_fini() to free the memory allocated by each matchpathcon_init_prefix() call.

That makes it slower than necessary and leaks memory.

See the bug report for the discussion.

Can we get this corrected in the upstream coreutils? Thanks.

Signature · 7 lines
Stephen Smalley
National Security Agency

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#4 lore raw

Stephen Smalley <[email protected]> wrote:
...

Quoted 8 lines

This issue came up recently again, see:
https://bugzilla.redhat.com/show_bug.cgi?id=447410

It appears that the patch that was merged into coreutils ends up calling matchpathcon_init_prefix() for each file being installed rather than once upon startup, and without calling matchpathcon_fini() to free the memory allocated by each matchpathcon_init_prefix() call.

Quoted 5 lines

That makes it slower than necessary and leaks memory.

See the bug report for the discussion.

Can we get this corrected in the upstream coreutils? Thanks.

Thanks for letting me know. The patch below should do the job. I didn't bother calling matchpathcon_fini, since its 6MB buffer is still reachable.

For now, I'm leaving that code ifdef'd out, because the performance penalty is still too high even on rawhide, when performing a few hundred to a thousand separate install commands (about a 20x hit, when installing to /usr). For reference, I did this:

  # matchpathcon code all ifdef'd out:
  $ touch k; time ( for i in $(seq 200); do; install k /usr/tmp/k ;done )
   0.08s user 0.30s system 97% cpu 0.391 total
  # matchpathcon code in use:
  $ touch k; time ( for i in $(seq 200); do; ./ginstall k /usr/tmp/k ;done )
   7.19s user 1.62s system 99% cpu 8.840 total
#5 lore raw
Quoted 25 lines · Jim Meyering

Stephen Smalley <[email protected]> wrote:
...

Quoted 8 lines

This issue came up recently again, see:
https://bugzilla.redhat.com/show_bug.cgi?id=447410

It appears that the patch that was merged into coreutils ends up calling matchpathcon_init_prefix() for each file being installed rather than once upon startup, and without calling matchpathcon_fini() to free the memory allocated by each matchpathcon_init_prefix() call.

Quoted 5 lines

That makes it slower than necessary and leaks memory.

See the bug report for the discussion.

Can we get this corrected in the upstream coreutils? Thanks.

Thanks for letting me know. The patch below should do the job. I didn't bother calling matchpathcon_fini, since its 6MB buffer is still reachable.

For now, I'm leaving that code ifdef'd out, because the performance penalty is still too high even on rawhide, when performing a few hundred to a thousand separate install commands (about a 20x hit, when installing to /usr). For reference, I did this:

Quoted 6 lines
 # matchpathcon code all ifdef'd out:
 $ touch k; time ( for i in $(seq 200); do; install k /usr/tmp/k ;done )
  0.08s user 0.30s system 97% cpu 0.391 total
 # matchpathcon code in use:
 $ touch k; time ( for i in $(seq 200); do; ./ginstall k /usr/tmp/k ;done )
  7.19s user 1.62s system 99% cpu 8.840 total

Just wondering, how much was the perf penalty before this patch?

-Eric

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#6 lore raw
Quoted 26 lines · Eric Paris
Quoted 25 lines · Jim Meyering

Stephen Smalley <[email protected]> wrote:
...

Quoted 8 lines

This issue came up recently again, see:
https://bugzilla.redhat.com/show_bug.cgi?id=447410

It appears that the patch that was merged into coreutils ends up calling matchpathcon_init_prefix() for each file being installed rather than once upon startup, and without calling matchpathcon_fini() to free the memory allocated by each matchpathcon_init_prefix() call.

Quoted 5 lines

That makes it slower than necessary and leaks memory.

See the bug report for the discussion.

Can we get this corrected in the upstream coreutils? Thanks.

Thanks for letting me know. The patch below should do the job. I didn't bother calling matchpathcon_fini, since its 6MB buffer is still reachable.

For now, I'm leaving that code ifdef'd out, because the performance penalty is still too high even on rawhide, when performing a few hundred to a thousand separate install commands (about a 20x hit, when installing to /usr). For reference, I did this:

Quoted 8 lines
Quoted 6 lines
 # matchpathcon code all ifdef'd out:
 $ touch k; time ( for i in $(seq 200); do; install k /usr/tmp/k ;done )
  0.08s user 0.30s system 97% cpu 0.391 total
 # matchpathcon code in use:
 $ touch k; time ( for i in $(seq 200); do; ./ginstall k /usr/tmp/k ;done )
  7.19s user 1.62s system 99% cpu 8.840 total

Just wondering, how much was the perf penalty before this patch?

For the single-file case, the patch I pushed makes no performance difference (assuming the if-0'd code is enabled), since matchpathcon is called only once.

In the multi-file case, the pre-patch performance penalty for enabling the ifdef'd-out code would range from probably-immeasurable (for just 2 or 3 files) to infinite, with enough files to make install exhaust virtual memory.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw
Quoted 12 lines · Ondrej Vasik
Quoted 4 lines · Jim Meyering <jim <at> meyering.net> writes:

In the multi-file case, the pre-patch performance penalty for enabling the ifdef'd-out code would range from probably-immeasurable (for just 2 or 3 files) to infinite, with enough files to make install exhaust virtual memory.

Actually even for 2-3 files (copied to /usr/temp dir) it was measurable. On my machine the performance impact is following:
Setdefaultcontext code if0'd: 0.0148 s.
Current patch(matchpathcon_init_prefix just once): 0.0666 s.
Old setdefaultcontext code: 0.1702 s.

Quoted 6 lines

For 100 files(copied to /usr/temp) it took 4.0342 s. - that's about 35,5 times more than with current patch from Jim and 128 times more than with if0'd setdefaultcontext code. And of course for more files it goes to infinity.

Problem is that disabled code is causing bugzilla tickets like
https://bugzilla.redhat.com/show_bug.cgi?id=319231 , so maybe the best solution

Perhaps you mean some other BZ? That one doesn't involve performance. To enable the code in question, just define this symbol for install.c:
ENABLE_WHEN_MATCHPATHCON_IS_MORE_EFFICIENT

Quoted 3 lines

to reduce performance impact would be to patch automake to install multiple files in one directory at once or something like that - to reduce performance impact of the ifdefed code on installation of big portions of files.

That will help, and part of it is already done in upstream automake:

2008-03-08 Ralf Wildenhues <[email protected]>

Use `install' with multiple files at once for some primaries. With nobase targets, at most 50 files are installed at once, to avoid quadratic string concatenation and line length limits. This isn't yet done with base targets. One hope is that there, the typical file name length is lower. If this turns out to be a problem, it should be revisited.

but that doesn't yet help when installing e.g., coreutils' 100 programs, since the existing code still loops, installing each individually. In a way, it has to, because with --program-transform-name, it may have to rename each one.

However, automake *can* (and probably will, now that I've proposed it) special-case the very common situation in which there is no --program-transform-name and $(EXEEXT) is empty. Maybe someone will propose a patch to do that. A 30x performance improvement is worth a small compromise for the common case.

However, the underlying problem still needs to be dealt with:
the outrageous expense of the matchpathcon function. Is anyone planning to address that?

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#9 lore raw
Quoted 50 lines · Jim Meyering
Quoted 12 lines · Ondrej Vasik
Quoted 4 lines · Jim Meyering <jim <at> meyering.net> writes:

In the multi-file case, the pre-patch performance penalty for enabling the ifdef'd-out code would range from probably-immeasurable (for just 2 or 3 files) to infinite, with enough files to make install exhaust virtual memory.

Actually even for 2-3 files (copied to /usr/temp dir) it was measurable. On my machine the performance impact is following:
Setdefaultcontext code if0'd: 0.0148 s.
Current patch(matchpathcon_init_prefix just once): 0.0666 s.
Old setdefaultcontext code: 0.1702 s.

Quoted 6 lines

For 100 files(copied to /usr/temp) it took 4.0342 s. - that's about 35,5 times more than with current patch from Jim and 128 times more than with if0'd setdefaultcontext code. And of course for more files it goes to infinity.

Problem is that disabled code is causing bugzilla tickets like
https://bugzilla.redhat.com/show_bug.cgi?id=319231 , so maybe the best solution

Perhaps you mean some other BZ? That one doesn't involve performance. To enable the code in question, just define this symbol for install.c:
ENABLE_WHEN_MATCHPATHCON_IS_MORE_EFFICIENT

Quoted 3 lines

to reduce performance impact would be to patch automake to install multiple files in one directory at once or something like that - to reduce performance impact of the ifdefed code on installation of big portions of files.

That will help, and part of it is already done in upstream automake:

2008-03-08 Ralf Wildenhues <[email protected]>

Use `install' with multiple files at once for some primaries. With nobase targets, at most 50 files are installed at once, to avoid quadratic string concatenation and line length limits. This isn't yet done with base targets. One hope is that there, the typical file name length is lower. If this turns out to be a problem, it should be revisited.

but that doesn't yet help when installing e.g., coreutils' 100 programs, since the existing code still loops, installing each individually. In a way, it has to, because with --program-transform-name, it may have to rename each one.

However, automake *can* (and probably will, now that I've proposed it) special-case the very common situation in which there is no --program-transform-name and $(EXEEXT) is empty. Maybe someone will propose a patch to do that. A 30x performance improvement is worth a small compromise for the common case.

Quoted 3 lines

However, the underlying problem still needs to be dealt with:
the outrageous expense of the matchpathcon function. Is anyone planning to address that?

There have been a number of small optimizations made over time, and we keep looking for other ways to improve the situation. There is also an experimental implementation in progress to replace the current use of pathname regexes with a simpler glob syntax (FCglob) that should help if it succeeds.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#10 lore raw
In reply to Jim Meyering
Quoted 18 lines · Jim Meyering

Hi,

This morning I noticed a flagrant difference in the speed of "make install" for the just-released gettext-0.17. It took 12(!) times longer on a rawhide system than on a usually-slower debian unstable system. (3min vs. 15s)

I tracked it down to the fact that on that rawhide system, I'm using the latest _upstream_ install binary from (SELinux-enabled) coreutils. The rawhide /usr/bin/install only ever calls matchpathcon (via setdefaultfilecon) when its final chown or chmod fails. Looks like a fortuitous bug in the rawhide patch. The version of install from upstream coreutils calls it every time, since that's the only thing that made sense to me when I redid coreutils' SELinux support. Considering no one can be relying on install's matchpathcon semantics, even in rawhide, I think it should not be the default, upstream -- especially considering the enormous performance hit.

Quoted 1 line

Any objections or suggestions from the SELinux folks?

Don't have a suggestion on the usage, but your overhead is coming from the loading of the file contexts database on each invocation of /bin/install. You could try to make the gettext installer more judicious about batching things into a single /bin/install command line.
Or make install a shell builtin, so that it's not a separate process.

There is a new handle-based set of calls that replaces matchpathcon() and friends. See include/selinux/label.h. If you're rewriting this part of the code, I'd recommend switching over. It won't affect performance too much I'm afraid, but maybe at some point there will be a daemon backend of some sort so it just makes an IPC call instead of loading the database in the library code.

Quoted 31 lines

Do you think coreutils' install should use matchpathcon at all? If so, either it'll have to be associated with a new option, or it'll have to be a lot more efficient. Yes, we've discussed this before, and you guys convinced me to provide the functionality for ease of use. But for those not familiar with the issues, note that you can get the same effect without the embedded matchpathcon call, simply by invoking install with a context provided by the matchpathcon _program_:

  install -Z $(matchpathcon $dest_file) SRC_FILE $dest_file

Can someone can look at making matchpathcon more efficient? I understand that making it the default would be nice, and if it's feasible to minimize the performance impact of that matchpathcon call, it'd be much better *not* to add a new option.

Unless I find a magic bullet, I'll disable install's use of matchpathcon for the upcoming test release.

Jim

In case you're interested in trying a recent snapshot,

http://meyering.net/cu/coreutils-6.9-ss.tar.gz
http://meyering.net/cu/coreutils-6.9-ss.tar.gz.sig

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

Signature · 7 lines
Eamon Walsh <[email protected]>
National Security Agency

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#11 lore raw
Quoted 6 lines · Eamon Walsh

Don't have a suggestion on the usage, but your overhead is coming from the loading of the file contexts database on each invocation of /bin/install. You could try to make the gettext installer more judicious about batching things into a single /bin/install command line. Or make install a shell builtin, so that it's not a separate process.

Thanks for the suggestions, but unfortunately, neither is feasible, since the Makefile install rules are generated by automake.

Quoted 6 lines

There is a new handle-based set of calls that replaces matchpathcon() and friends. See include/selinux/label.h. If you're rewriting this part of the code, I'd recommend switching over. It won't affect performance too much I'm afraid, but maybe at some point there will be a daemon backend of some sort so it just makes an IPC call instead of loading the database in the library code.

Good to know.
I'll hold off for now, since there'd be no immediate benefit.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.