From 6ed51c0e8542922f8ceb2659a7921e0af096d734 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Fri, 4 Dec 2015 11:34:42 -0800 Subject: [PATCH] add checks for initialization for system properties If a __system_property* function is called before __system_properties_init() then the app will will abort. This commit returns either an error code or a safe return value instead. Bug 26027140 Change-Id: I95ffd143e9563658ab67a397991e84fb4c46ab77 --- libc/bionic/system_properties.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/libc/bionic/system_properties.cpp b/libc/bionic/system_properties.cpp index 3fd41d733..9b44ff20d 100644 --- a/libc/bionic/system_properties.cpp +++ b/libc/bionic/system_properties.cpp @@ -1006,6 +1006,10 @@ unsigned int __system_property_area_serial() const prop_info *__system_property_find(const char *name) { + if (!__system_property_area__) { + return nullptr; + } + if (__predict_false(compat_mode)) { return __system_property_find_compat(name); } @@ -1091,11 +1095,15 @@ int __system_property_set(const char *key, const char *value) int __system_property_update(prop_info *pi, const char *value, unsigned int len) { - prop_area *pa = __system_property_area__; - if (len >= PROP_VALUE_MAX) return -1; + prop_area* pa = __system_property_area__; + + if (!pa) { + return -1; + } + uint32_t serial = atomic_load_explicit(&pi->serial, memory_order_relaxed); serial |= 1; atomic_store_explicit(&pi->serial, serial, memory_order_relaxed); @@ -1129,6 +1137,10 @@ int __system_property_add(const char *name, unsigned int namelen, if (namelen < 1) return -1; + if (!__system_property_area__) { + return -1; + } + prop_area* pa = get_prop_area_for_name(name); if (!pa) { @@ -1168,6 +1180,10 @@ unsigned int __system_property_wait_any(unsigned int serial) prop_area *pa = __system_property_area__; uint32_t my_serial; + if (!pa) { + return 0; + } + do { __futex_wait(pa->serial(), serial, NULL); my_serial = atomic_load_explicit(pa->serial(), memory_order_acquire); @@ -1191,6 +1207,10 @@ const prop_info *__system_property_find_nth(unsigned n) int __system_property_foreach(void (*propfn)(const prop_info *pi, void *cookie), void *cookie) { + if (!__system_property_area__) { + return -1; + } + if (__predict_false(compat_mode)) { return __system_property_foreach_compat(propfn, cookie); }