diff options
author | William Manley <will@williammanley.net> | 2017-01-10 15:13:56 +0000 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2017-01-29 03:22:46 -0500 |
commit | 6bf55255e8a688997973d9d40755fa999eda3791 (patch) | |
tree | 1441110d657baa9e406281babc38b3de551b6813 /glnx-xattrs.c | |
parent | 597f03b4058a4d495252ee5479371c0b428fe40f (diff) | |
download | libglnx-6bf55255e8a688997973d9d40755fa999eda3791.tar.gz |
listxattr: Don't assume that first call to listxattr gives correct size
To get the right sized buffer to pass to `flistattr` and `llistattr` we
first call them with a zero byte buffer. They then return the number of
bytes they'll actually need to operate. We would `malloc` and then call
again assuming that the size we got originally was correct.
On my computer at least this isn't always the case. I've seen instances
where the first call returns 23B, but then on the second one returns no
data at all. Getting these non-existant xattrs would then cause ostree
to fail.
I'm not sure why it's behaving this way on my machine. I suspect its some
interaction with overlayfs but I haven't proven this.
Diffstat (limited to 'glnx-xattrs.c')
-rw-r--r-- | glnx-xattrs.c | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/glnx-xattrs.c b/glnx-xattrs.c index b5a5922..d50b3c2 100644 --- a/glnx-xattrs.c +++ b/glnx-xattrs.c @@ -135,7 +135,7 @@ get_xattrs_impl (const char *path, GError **error) { gboolean ret = FALSE; - ssize_t bytes_read; + ssize_t bytes_read, real_size; glnx_free char *xattr_names = NULL; glnx_free char *xattr_names_canonical = NULL; GVariantBuilder builder; @@ -158,15 +158,19 @@ get_xattrs_impl (const char *path, else if (bytes_read > 0) { xattr_names = g_malloc (bytes_read); - if (llistxattr (path, xattr_names, bytes_read) < 0) + real_size = llistxattr (path, xattr_names, bytes_read); + if (real_size < 0) { glnx_set_prefix_error_from_errno (error, "%s", "llistxattr"); goto out; } - xattr_names_canonical = canonicalize_xattrs (xattr_names, bytes_read); - - if (!read_xattr_name_array (path, -1, xattr_names_canonical, bytes_read, &builder, error)) - goto out; + else if (real_size > 0) + { + xattr_names_canonical = canonicalize_xattrs (xattr_names, real_size); + + if (!read_xattr_name_array (path, -1, xattr_names_canonical, real_size, &builder, error)) + goto out; + } } ret_xattrs = g_variant_builder_end (&builder); @@ -202,7 +206,7 @@ glnx_fd_get_all_xattrs (int fd, GError **error) { gboolean ret = FALSE; - ssize_t bytes_read; + ssize_t bytes_read, real_size; glnx_free char *xattr_names = NULL; glnx_free char *xattr_names_canonical = NULL; GVariantBuilder builder; @@ -225,15 +229,19 @@ glnx_fd_get_all_xattrs (int fd, else if (bytes_read > 0) { xattr_names = g_malloc (bytes_read); - if (flistxattr (fd, xattr_names, bytes_read) < 0) + real_size = flistxattr (fd, xattr_names, bytes_read); + if (real_size < 0) { glnx_set_prefix_error_from_errno (error, "%s", "flistxattr"); goto out; } - xattr_names_canonical = canonicalize_xattrs (xattr_names, bytes_read); - - if (!read_xattr_name_array (NULL, fd, xattr_names_canonical, bytes_read, &builder, error)) - goto out; + else if (real_size > 0) + { + xattr_names_canonical = canonicalize_xattrs (xattr_names, real_size); + + if (!read_xattr_name_array (NULL, fd, xattr_names_canonical, real_size, &builder, error)) + goto out; + } } ret_xattrs = g_variant_builder_end (&builder); |