libselinux: maintain mode even if umask is tighter

When certain programs were run which created new files they would get
default permissions based on the current users umask.  However these
files should get the same permissions as those files which they
replaced.  Do that.

Patch from: Stephen Smalley

Signed-off-by: Eric Paris <eparis@redhat.com>
Acked-by: Dan Walsh <dwalsh@redhat.com>
This commit is contained in:
Dan Walsh 2011-10-20 15:13:17 -04:00 committed by Eric Paris
parent 023c9c1fde
commit 86e8daafc3

View File

@ -495,6 +495,7 @@ static int semanage_copy_file(const char *src, const char *dst, mode_t mode)
int in, out, retval = 0, amount_read, n, errsv = errno;
char tmp[PATH_MAX];
char buf[4192];
mode_t mask;
n = snprintf(tmp, PATH_MAX, "%s.tmp", dst);
if (n < 0 || n >= PATH_MAX)
@ -506,13 +507,16 @@ static int semanage_copy_file(const char *src, const char *dst, mode_t mode)
if (!mode)
mode = S_IRUSR | S_IWUSR;
mask = umask(0);
if ((out = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, mode)) == -1) {
umask(mask);
errsv = errno;
close(in);
retval = -1;
goto out;
}
umask(mask);
while (retval == 0 && (amount_read = read(in, buf, sizeof(buf))) > 0) {
if (write(out, buf, amount_read) < 0) {
errsv = errno;