diff options
author | Michael Vogt <mvo@ubuntu.com> | 2014-04-09 10:12:10 +0200 |
---|---|---|
committer | Michael Vogt <mvo@ubuntu.com> | 2014-04-10 08:59:47 +0200 |
commit | f22b65b47990237bd5d9a1c171919c3059fbd9b0 (patch) | |
tree | e22e54c3256554884b1042a2088638207b169ec3 /test | |
parent | 5572f6bdcb947e11f32e2a035438d9d3899ad46d (diff) |
Fix insecure file permissions when using FileFd with OpenMode::Atomic
Commit 7335eebea6dd43581d4650a8818b06383ab89901 introduced a bug
that caused FileFd to create insecure permissions when FileFd::Atomic
is used. This commit fixes the permissions and adds a test.
The bug is most likely caused by the confusing "Perm" parameter
that is passed to Open() - its not the file permissions but intead
the "mode" part of open/creat.
Diffstat (limited to 'test')
-rw-r--r-- | test/libapt/fileutl_test.cc | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc index 8da832ba9..1d1a1a1b8 100644 --- a/test/libapt/fileutl_test.cc +++ b/test/libapt/fileutl_test.cc @@ -6,13 +6,44 @@ #include <string> #include <vector> #include <stdlib.h> +#include <sys/stat.h> #include "assert.h" +// regression test for permission bug LP: #1304657 +static bool +TestFileFdOpenPermissions(mode_t a_umask, mode_t ExpectedFilePermission) +{ + FileFd f; + struct stat buf; + static const char* fname = "test.txt"; + + umask(a_umask); + f.Open(fname, FileFd::ReadWrite|FileFd::Atomic); + f.Close(); + if (stat(fname, &buf) < 0) + { + _error->Errno("stat", "failed to stat"); + _error->DumpErrors(); + return false; + } + unlink(fname); + equals(buf.st_mode & 0777, ExpectedFilePermission); + return true; +} + int main() { std::vector<std::string> files; + if (TestFileFdOpenPermissions(0002, 0664) == false || + TestFileFdOpenPermissions(0022, 0644) == false || + TestFileFdOpenPermissions(0077, 0600) == false || + TestFileFdOpenPermissions(0026, 0640) == false) + { + return 1; + } + // normal match files = Glob("*.lst"); if (files.size() != 1) |