Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1f09bdaf authored by Dan Albert's avatar Dan Albert
Browse files

Protect from eng vs userdebug build breaks.

Using a const bool rather than an ifdef means the compiler can still
protect us from breaking code paths that aren't included in every
build variant.

Change-Id: Ic45c8fb52cd66c3ce090d760cdb92104e31265f5
parent fe685787
Loading
Loading
Loading
Loading
+66 −60
Original line number Diff line number Diff line
@@ -14,24 +14,29 @@
 * limitations under the License.
 */

#include "sysdeps.h"

#define  TRACE_TAG  TRACE_ADB
#include "adb.h"

#include <stdio.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/stat.h>

#define  TRACE_TAG  TRACE_ADB
#include "adb.h"
#include "cutils/properties.h"
#include "ext4_sb.h"
#include <fs_mgr.h>
#include "fs_mgr.h"
#include "sysdeps.h"

#define FSTAB_PREFIX "/fstab."
struct fstab *fstab;

#ifdef ALLOW_ADBD_DISABLE_VERITY
static const bool kAllowDisableVerity = true;
#else
static const bool kAllowDisableVerity = false;
#endif

__attribute__((__format__(printf, 2, 3))) __nonnull((2))
static void write_console(int fd, const char* format, ...)
{
@@ -44,7 +49,6 @@ static void write_console(int fd, const char* format, ...)
    adb_write(fd, buffer, strnlen(buffer, sizeof(buffer)));
}

#ifdef ALLOW_ADBD_DISABLE_VERITY
static int get_target_device_size(int fd, const char *blk_device,
                                  uint64_t *device_size)
{
@@ -148,10 +152,10 @@ static int set_verity_enabled_state(int fd, const char *block_device,
    }

    if (adb_write(device, &new_magic, sizeof(new_magic)) != sizeof(new_magic)) {
        write_console(fd, "Could not set verity %s flag on device %s with error %s\n",
        write_console(
            fd, "Could not set verity %s flag on device %s with error %s\n",
            enable ? "enabled" : "disabled",
                      block_device,
                      strerror(errno));
            block_device, strerror(errno));
        goto errout;
    }

@@ -164,12 +168,11 @@ errout:
        adb_close(device);
    return retval;
}
#endif

void set_verity_enabled_state_service(int fd, void* cookie)
{
    bool enable = (cookie != NULL);
#ifdef ALLOW_ADBD_DISABLE_VERITY
    if (kAllowDisableVerity) {
        char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
        char propbuf[PROPERTY_VALUE_MAX];
        int i;
@@ -183,12 +186,14 @@ void set_verity_enabled_state_service(int fd, void* cookie)

        property_get("ro.debuggable", propbuf, "0");
        if (strcmp(propbuf, "1")) {
        write_console(fd, "verity cannot be disabled/enabled - USER build\n");
            write_console(
                fd, "verity cannot be disabled/enabled - USER build\n");
            goto errout;
        }

        property_get("ro.hardware", propbuf, "");
    snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);
        snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s",
                 propbuf);

        fstab = fs_mgr_read_fstab(fstab_filename);
        if (!fstab) {
@@ -201,21 +206,22 @@ void set_verity_enabled_state_service(int fd, void* cookie)
        for (i = 0; i < fstab->num_entries; i++) {
            if(fs_mgr_is_verified(&fstab->recs[i])) {
                if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
                                          fstab->recs[i].mount_point, enable)) {
                                              fstab->recs[i].mount_point,
                                              enable)) {
                    any_changed = true;
                }
           }
        }

        if (any_changed) {
        write_console(fd,
                      "Now reboot your device for settings to take effect\n");
            write_console(
                fd, "Now reboot your device for settings to take effect\n");
        }
errout:
#else
    } else {
        write_console(fd, "%s-verity only works for userdebug builds\n",
                      enable ? "enable" : "disable");
#endif
    }

errout:
    adb_close(fd);
}