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

Commit a76f057a authored by Dan Albert's avatar Dan Albert Committed by Gerrit Code Review
Browse files

Merge "Protect from eng vs userdebug build breaks."

parents fe685787 1f09bdaf
Loading
Loading
Loading
Loading
+66 −60
Original line number Original line Diff line number Diff line
@@ -14,24 +14,29 @@
 * limitations under the License.
 * 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 <fcntl.h>
#include <inttypes.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 "cutils/properties.h"
#include "ext4_sb.h"
#include "ext4_sb.h"
#include <fs_mgr.h>
#include "fs_mgr.h"
#include "sysdeps.h"


#define FSTAB_PREFIX "/fstab."
#define FSTAB_PREFIX "/fstab."
struct fstab *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))
__attribute__((__format__(printf, 2, 3))) __nonnull((2))
static void write_console(int fd, const char* format, ...)
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)));
    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,
static int get_target_device_size(int fd, const char *blk_device,
                                  uint64_t *device_size)
                                  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)) {
    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",
            enable ? "enabled" : "disabled",
                      block_device,
            block_device, strerror(errno));
                      strerror(errno));
        goto errout;
        goto errout;
    }
    }


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


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


        property_get("ro.debuggable", propbuf, "0");
        property_get("ro.debuggable", propbuf, "0");
        if (strcmp(propbuf, "1")) {
        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;
            goto errout;
        }
        }


        property_get("ro.hardware", propbuf, "");
        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);
        fstab = fs_mgr_read_fstab(fstab_filename);
        if (!fstab) {
        if (!fstab) {
@@ -201,21 +206,22 @@ void set_verity_enabled_state_service(int fd, void* cookie)
        for (i = 0; i < fstab->num_entries; i++) {
        for (i = 0; i < fstab->num_entries; i++) {
            if(fs_mgr_is_verified(&fstab->recs[i])) {
            if(fs_mgr_is_verified(&fstab->recs[i])) {
                if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
                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;
                    any_changed = true;
                }
                }
           }
           }
        }
        }


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


errout:
    adb_close(fd);
    adb_close(fd);
}
}