Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate Pixel's new repair mode feature #216

Closed
chenxiaolong opened this issue Dec 7, 2023 · 12 comments · Fixed by #243
Closed

Investigate Pixel's new repair mode feature #216

chenxiaolong opened this issue Dec 7, 2023 · 12 comments · Fixed by #243
Assignees

Comments

@chenxiaolong
Copy link
Owner

The December 2023 Pixel OTA update added support for a "repair mode". I'd like to investigate to see if this has any impact to how things are signed for AVB.

The feature is not open source. There are parts of the implementation in /system_ext/priv-app/SettingsGoogle and /system_ext/app/RepairMode. At first glance, it seems to be implemented on top of Android's DSU mechanism.

@chenxiaolong chenxiaolong self-assigned this Dec 7, 2023
@chenxiaolong
Copy link
Owner Author

chenxiaolong commented Dec 7, 2023

Repair mode is fully working in an avbroot setup, but with a big caveat (see bottom of post).

But first, this is how the feature is implemented:

  1. When repair mode is turned on, it creates a new DSU (dynamic system update) slot named repairmode.lock:

    DynamicSystemManager.startInstallation("repairmode.lock")
  2. It then writes an empty userdata partition image in /data/gsi/dsu/repairmode.lock/. The minimum size of the image file is 2 GiB and the maximum size is min(<free space> - 300 MiB, 10 GiB) rounded down to the nearest GiB.

    DynamicSystemManager.createPartition("userdata", size, /* readOnly */ false)
    DynamicSystemManager.closePartition()
    DynamicSystemManager.finishInstallation()

    Because no other partition images are created, the original system/system_ext/product/vendor/etc. are used as-is.

  3. If the DSU creation is successful, then it marks the new repairmode.lock slot as the default slot for all future reboots.

    DynamicSystemManager.setEnable(/* enable */ true, /* oneShot */ false)

    If you're quick, running gsi_tool before the device has a chance to reboot will output:

    # gsi_tool status
    installed
    enabled
    [0] repairmode.lock
    installed: userdata_gsi
    AVB public key (sha1): [NONE]
    
  4. After rebooting, the device will mount the new userdata image at /data instead of the real userdata partition. Even though the image lives on the real partition, the real partiition is never mounted. This works because the image creation procedure uses Linux's fiemap API to record which sectors/blocks the image file occupies on disk. When booted into repair mode, only those specific blocks are accessed (via dm-linear).

  5. The (proprietary) RepairMode app will set up Android's device policy mechanism to disable the initial setup wizard and gray out the factory reset and OEM unlocking options.

  6. The device remains in repair mode until the user exits it via the notification. This will configure the system to boot back into the original system and the repairmode.lock DSU slot will be deleted after rebooting.


WARNING!

There's is one big caveat with avbroot + repair mode. Repair mode will also be rooted! All it takes is installing the Magisk/KernelSU app and root access will be granted. This is problematic because with root access, it's trivial for someone to manually mount the original userdata partition and install a malicious Magisk/KernelSU module. Emphasis on trivial since repair mode has no authentication (because of what the feature is intended for). A malicious module installed this way would persist after exiting repair mode.

EDIT: Per @pascallj's suggestion below, a good way to work around this is by sideloading an unrooted avbroot-signed OTA prior to entering repair mode. This can be done by patching with the --rootless option. After the device is repaired, exit repair mode and sideload the rooted patched OTA again. Switching between unrooted and rooted Android builds this way does not require wiping data.

EDIT 2: This helps close the unauthenticated root access vulnerability, but it's not perfectly safe. In repair mode, Google's original OTA updater is enabled by default and could potentially run. avbroot's clearotacerts module won't be active to prevent this since the OS would be unrooted. There is currently no solution for this. Best to just flash the stock OS before sending the device in for repairs.

@chenxiaolong chenxiaolong pinned this issue Dec 7, 2023
@pascallj
Copy link
Contributor

pascallj commented Dec 7, 2023

Thanks for researching this! When I noticed this feature in the feature drop, it immediately peaked my interest.

I agree that giving a device with avbroot to any sort of official repair shop is a bad idea. It will raise red flags and I think they will just reset the device then anyway. However if you're just getting your screen replaced at the local phone shop around the corner, this mode would still be sufficient.

Flashing the stock OS will force you to wipe your device as we have established that it cannot (or won't) decrypt userdata written with custom keys. Would flashing a rootless variant of the system fix this issue? There won't be any way to get root on the system now and because the bootloader is locked it is impossible to revert to the other version. You can flash a root-enabled variant when the device returns and everything should work as is.

@chenxiaolong
Copy link
Owner Author

Would flashing a rootless variant of the system fix this issue?

That's brilliant! I didn't think of that at all. Yes, that completely solves the problem. Switching between avbroot-signed rootless and rooted builds is just an adb sideload away. I'll update my message above to suggest that.

@pascallj
Copy link
Contributor

pascallj commented Dec 7, 2023

One downside I was later thinking of, is that the 'clearotacerts' module is not running. Therefore maybe the system might get an accidental OTA update (if that works in repair mode) which breaks the system.

@chenxiaolong
Copy link
Owner Author

chenxiaolong commented Dec 7, 2023

I'll check AOSP tomorrow and see if I can find a definitive answer to that. My guess is that update_engine or something would hopefully disable itself while booted into an DSU. When booted into a DSU slot, any partition (eg. system) might be an arbitrary image. There are also post-install scripts that run in the host environment (Android normally or recovery if sideloading) when an OTA is installed. A DSU is not guaranteed to have a proper environment for that. It'd be pretty crazy/amazing if update_engine somehow worked.

@chenxiaolong
Copy link
Owner Author

I was wrong. I tested with Custota and it looks like OTA updates go through just fine in repair mode. It also looks like repair mode enables the OEM updater app too, which I suppose makes sense since that's the default. I don't see a good solution around this.

I was trying finding a way to do what the clearotacerts module does without Magisk/KernelSU, but after looking at the code for /system/bin/init, I highly doubt this is possible without wrapping the binary like what Magisk does. I think that's a bit too invasive and may conflict with Magisk.

I can't think of a way to use repair mode that's 100% safe against the accidental OTA update scenario at the moment.

@pascallj
Copy link
Contributor

pascallj commented Dec 8, 2023

Thanks for testing! Avbroot supports repacking of images now right? What if we just replace the otacerts.zip and repack the image?

@chenxiaolong
Copy link
Owner Author

That would be the best solution. avbroot can deal with all the AVB, dm-verify, and FEC parts, but it doesn't have the ability to write to an ext4 image or recreate an erofs image (since Android's copy of otacerts.zip lives in system.img). I'm not aware of any libraries that can read/write those filesystems in userspace.

@pascallj
Copy link
Contributor

pascallj commented Dec 8, 2023

I'm not aware of any libraries that can read/write those filesystems in userspace.

In userspace, as in userspace on the device avbroot is running on?

We can just corrupt the file 🙈 Ext4 doesn't have checksums as far as I'm aware.

@chenxiaolong
Copy link
Owner Author

In userspace, as in userspace on the device avbroot is running on?

I meant on the system that's running avbroot ota patch. The filesystem would have to be modified and then avbroot can recompute the dm-verity hashes and FEC.

We can just corrupt the file 🙈 Ext4 doesn't have checksums as far as I'm aware.

😂 I guess that is indeed theoretically possible. Still would require some sort of ext4/erofs parser to find the blocks where the file is located though.

(If there actually is a good way to find the block(s) containing otacerts.zip, we could do better than corrupting it. We could insert one with the user's key and pad out the file to equal the original size. avbroot's generated otacerts.zip is usually a few hundred bytes smaller than Google's original one.)

@chenxiaolong
Copy link
Owner Author

Well, it turns out recovery's copy of otacerts.zip is byte-for-byte identical to the copy inside system.img. Blindly searching for those bytes inside system.img and replacing them actually works: https://gist.github.com/chenxiaolong/da42491c0b7d4fb8262b6fc3c2850777.

I tested with ext4 and uncompressed erofs. fsck passes, the filesystem mounts, and the replaced otacerts.zip data shows up as expected.

Although this actually works well, I probably won't implement this because:

  • Patching would be slow since system.img would need to be extracted, modified, and recompressed. Inside a temp directory, the image would need to be fully read 3 times and fully written once in order to make the modifications and regenerate the dm-verity metadata. This would probably increase the patching times by an order of magnitude.
  • This doesn't work at all for compressed erofs. I wouldn't be surprised if Google switches to this in the near future. I believe Samsung's newest devices already use it.

It sure would be nice if we could just:

mount --bind <recovery's otacerts> <system's otacerts>

during init's first stage... There's just no easy way to hook into that process.

@pascallj
Copy link
Contributor

Interesting! I agree, it is not worth implementing it this way. It's a very niche use case.

chenxiaolong added a commit that referenced this issue Dec 24, 2023
Previously, overriding otacerts.zip in the system partition required the
user to flash a Magisk/KernelSU module that would bind mount over the
file during boot. While this worked well enough, it's insufficient for
unrooted setups, which has become more important since unrooting is the
only safe way to use the new OEM repair mode feature. With the stock
otacerts.zip, the OEM's default OTA updater app could run and install an
OS upgrade that's not signed by the user's key.

With this key, the raw otacerts.zip bytes in the system partition are
directly replaced with a new zip that contains the user's certificate.
This method was inspired by @pascallj's comment in #216 suggestiing
intentionally corrupting the otacerts.zip data in the filesystem.

Because avbroot does not have filesystem parsers for ext4/f2fs/erofs, we
rely on a heuristic-based search on the raw filesystem image. The file
is always smaller than one block (which is at least 4096 bytes on all
known devices), so the file data is stored continguously on disk and in
the case of erofs, won't be compressed. None of the three filesystems
are copy-on-write and thus, have no filesystem-level data checksums. For
the dm-verity layer one level up, avbroot already knows how to recompute
the hash tree and FEC data.

Since the new approach is doing a raw search and replace, the old and
new files must have the same size. When the new zip is smaller, null
bytes are added to the zip archive comment field to pad to the correct
size. When the new zip is larger, avbroot will attempt the following to
try and make the file size smaller:

1. Enable zip deflate compression
2. Strip the X.509 signature from the certificate
3. Clear out the issuer RDN sequence from the certificate
4. Clear out the subject RDN sequence from the certificate

The latter three changes work because Android never performs any PKI
operations with the certificate. There is no CA certificate chain. The
X.509 certificate file is nothing more than a way to transport an RSA
public key.

avbroot requires the user's key to be RSA 4096. If the original zip had
the same key size, then none of these shrinking methods are needed. If
it contained an RSA 2048 key, then the first two modifications are
usually sufficient. The latter two modifications should only be needed
if the user picked a really long subject value when generating the
certificate.

With these new changes, the OTA patching time will approximately double
on a system with an SSD and modern CPU. This is dominated by the time it
takes to XZ-compress the system partition image. The compression is
already parallelized and scales linearly with the number of cores.
There's likely not much more that can be done to further speed this up.

Finally, these new changes are currently excluded from the e2e tests
because including the system partition in the stripped OTAs would
increase the file size by an order of magnitude. This could potentially
be solved in the future by generating our own small OTAs to use for
testing instead of running against real device OTAs.

Fixes: #225

Signed-off-by: Andrew Gunnerson <[email protected]>
chenxiaolong added a commit that referenced this issue Dec 24, 2023
Previously, overriding otacerts.zip in the system partition required the
user to flash a Magisk/KernelSU module that would bind mount over the
file during boot. While this worked well enough, it's insufficient for
unrooted setups, which has become more important since unrooting is the
only safe way to use the new OEM repair mode feature. With the stock
otacerts.zip, the OEM's default OTA updater app could run and install an
OS upgrade that's not signed by the user's key.

With this key, the raw otacerts.zip bytes in the system partition are
directly replaced with a new zip that contains the user's certificate.
This method was inspired by @pascallj's comment in #216 suggesting
intentionally corrupting the otacerts.zip data in the filesystem.

Because avbroot does not have filesystem parsers for ext4/f2fs/erofs, we
rely on a heuristic-based search on the raw filesystem image. The file
is always smaller than one block (which is at least 4096 bytes on all
known devices), so the file data is stored contiguously on disk and in
the case of erofs, won't be compressed. None of the three filesystems
are copy-on-write and thus, have no filesystem-level data checksums. For
the dm-verity layer one level up, avbroot already knows how to recompute
the hash tree and FEC data.

Since the new approach is doing a raw search and replace, the old and
new files must have the same size. When the new zip is smaller, null
bytes are added to the zip archive comment field to pad to the correct
size. When the new zip is larger, avbroot will attempt the following to
try and make the file size smaller:

1. Enable zip deflate compression
2. Strip the X.509 signature from the certificate
3. Clear out the issuer RDN sequence from the certificate
4. Clear out the subject RDN sequence from the certificate

The latter three changes work because Android never performs any PKI
operations with the certificate. There is no CA certificate chain. The
X.509 certificate file is nothing more than a way to transport an RSA
public key.

avbroot requires the user's key to be RSA 4096. If the original zip had
the same key size, then none of these shrinking methods are needed. If
it contained an RSA 2048 key, then the first two modifications are
usually sufficient. The latter two modifications should only be needed
if the user picked a really long subject value when generating the
certificate.

With these new changes, the OTA patching time will approximately double
on a system with an SSD and modern CPU. This is dominated by the time it
takes to XZ-compress the system partition image. The compression is
already parallelized and scales linearly with the number of cores.
There's likely not much more that can be done to further speed this up.

Finally, these new changes are currently excluded from the e2e tests
because including the system partition in the stripped OTAs would
increase the file size by an order of magnitude. This could potentially
be solved in the future by generating our own small OTAs to use for
testing instead of running against real device OTAs.

Fixes: #225

Signed-off-by: Andrew Gunnerson <[email protected]>
chenxiaolong added a commit that referenced this issue Dec 24, 2023
Previously, overriding otacerts.zip in the system partition required the
user to flash a Magisk/KernelSU module that would bind mount over the
file during boot. While this worked well enough, it's insufficient for
unrooted setups, which has become more important since unrooting is the
only safe way to use the new OEM repair mode feature. With the stock
otacerts.zip, the OEM's default OTA updater app could run and install an
OS upgrade that's not signed by the user's key.

With this commit, the raw otacerts.zip bytes in the system partition are
directly replaced with a new zip that contains the user's certificate.
This method was inspired by @pascallj's comment in #216 suggesting
intentionally corrupting the otacerts.zip data in the filesystem.

Because avbroot does not have filesystem parsers for ext4/f2fs/erofs, we
rely on a heuristic-based search on the raw filesystem image. The file
is always smaller than one block (which is at least 4096 bytes on all
known devices), so the file data is stored contiguously on disk and in
the case of erofs, won't be compressed. None of the three filesystems
are copy-on-write and thus, have no filesystem-level data checksums. For
the dm-verity layer one level up, avbroot already knows how to recompute
the hash tree and FEC data.

To ensure that there are no false positives, any match that the search
finds must correctly parse as a valid zip and every entry within the zip
must have a filename that ends in .x509.pem. This matches what
update_engine expects from a proper otacerts.zip file.

Since the new approach is doing a raw search and replace, the old and
new files must have the same size. When the new zip is smaller, null
bytes are added to the zip archive comment field to pad to the correct
size. When the new zip is larger, avbroot will attempt the following to
try and make the file size smaller:

1. Enable zip deflate compression
2. Strip the X.509 signature from the certificate
3. Clear out the issuer RDN sequence from the certificate
4. Clear out the subject RDN sequence from the certificate

The latter three changes work because Android never performs any PKI
operations with the certificate. There is no CA certificate chain. The
X.509 certificate file is nothing more than a way to transport an RSA
public key.

avbroot requires the user's key to be RSA 4096. If the original zip had
the same key size, then none of these shrinking methods are needed. If
it contained an RSA 2048 key, then the first two modifications are
usually sufficient. The latter two modifications should only be needed
if the user picked a really long subject value when generating the
certificate.

With these new changes, the OTA patching time will approximately double
on a system with an SSD and modern CPU. This is dominated by the time it
takes to XZ-compress the system partition image. The compression is
already parallelized and scales linearly with the number of cores.
There's likely not much more that can be done to further speed this up.

Finally, these new changes are currently excluded from the e2e tests
because including the system partition in the stripped OTAs would
increase the file size by an order of magnitude. This could potentially
be solved in the future by generating our own small OTAs to use for
testing instead of running against real device OTAs.

Fixes: #225

Signed-off-by: Andrew Gunnerson <[email protected]>
chenxiaolong added a commit that referenced this issue Dec 24, 2023
Previously, overriding otacerts.zip in the system partition required the
user to flash a Magisk/KernelSU module that would bind mount over the
file during boot. While this worked well enough, it's insufficient for
unrooted setups, which has become more important since unrooting is the
only safe way to use the new OEM repair mode feature. With the stock
otacerts.zip, the OEM's default OTA updater app could run and install an
OS upgrade that's not signed by the user's key.

With this commit, the raw otacerts.zip bytes in the system partition are
directly replaced with a new zip that contains the user's certificate.
This method was inspired by @pascallj's comment in #216 suggesting
intentionally corrupting the otacerts.zip data in the filesystem.

Because avbroot does not have filesystem parsers for ext4/f2fs/erofs, we
rely on a heuristic-based search on the raw filesystem image. The file
is always smaller than one block (which is at least 4096 bytes on all
known devices), so the file data is stored contiguously on disk and in
the case of erofs, won't be compressed. None of the three filesystems
are copy-on-write and thus, have no filesystem-level data checksums. For
the dm-verity layer one level up, avbroot already knows how to recompute
the hash tree and FEC data.

To ensure that there are no false positives, any match that the search
finds must correctly parse as a valid zip and every entry within the zip
must have a filename that ends in .x509.pem. This matches what
update_engine expects from a proper otacerts.zip file.

Since the new approach is doing a raw search and replace, the old and
new files must have the same size. When the new zip is smaller, null
bytes are added to the zip archive comment field to pad to the correct
size. When the new zip is larger, avbroot will attempt the following to
try and make the file size smaller:

1. Enable zip deflate compression
2. Strip the X.509 signature from the certificate
3. Clear out the issuer RDN sequence from the certificate
4. Clear out the subject RDN sequence from the certificate

The latter three changes work because Android never performs any PKI
operations with the certificate. There is no CA certificate chain. The
X.509 certificate file is nothing more than a way to transport an RSA
public key.

avbroot requires the user's key to be RSA 4096. If the original zip had
the same key size, then none of these shrinking methods are needed. If
it contained an RSA 2048 key, then the first two modifications are
usually sufficient. The latter two modifications should only be needed
if the user picked a really long subject value when generating the
certificate.

With these new changes, the OTA patching time will approximately double
on a system with an SSD and modern CPU. This is dominated by the time it
takes to XZ-compress the system partition image. The compression is
already parallelized and scales linearly with the number of cores.
There's likely not much more that can be done to further speed this up.

Finally, these new changes are currently excluded from the e2e tests
because including the system partition in the stripped OTAs would
increase the file size by an order of magnitude. This could potentially
be solved in the future by generating our own small OTAs to use for
testing instead of running against real device OTAs.

Fixes: #225

Signed-off-by: Andrew Gunnerson <[email protected]>
chenxiaolong added a commit that referenced this issue Dec 24, 2023
Previously, overriding otacerts.zip in the system partition required the
user to flash a Magisk/KernelSU module that would bind mount over the
file during boot. While this worked well enough, it's insufficient for
unrooted setups, which has become more important since unrooting is the
only safe way to use the new OEM repair mode feature. With the stock
otacerts.zip, the OEM's default OTA updater app could run and install an
OS upgrade that's not signed by the user's key.

With this commit, the raw otacerts.zip bytes in the system partition are
directly replaced with a new zip that contains the user's certificate.
This method was inspired by @pascallj's comment in #216 suggesting
intentionally corrupting the otacerts.zip data in the filesystem.

Because avbroot does not have filesystem parsers for ext4/f2fs/erofs, we
rely on a heuristic-based search on the raw filesystem image. The file
is always smaller than one block (which is at least 4096 bytes on all
known devices), so the file data is stored contiguously on disk and in
the case of erofs, won't be compressed. None of the three filesystems
are copy-on-write and thus, have no filesystem-level data checksums. For
the dm-verity layer one level up, avbroot already knows how to recompute
the hash tree and FEC data.

To ensure that there are no false positives, any match that the search
finds must correctly parse as a valid zip and every entry within the zip
must have a filename that ends in .x509.pem. This matches what
update_engine expects from a proper otacerts.zip file.

Since the new approach is doing a raw search and replace, the old and
new files must have the same size. When the new zip is smaller, null
bytes are added to the zip archive comment field to pad to the correct
size. When the new zip is larger, avbroot will attempt the following to
try and make the file size smaller:

1. Enable zip deflate compression
2. Strip the X.509 signature from the certificate
3. Clear out the issuer RDN sequence from the certificate
4. Clear out the subject RDN sequence from the certificate

The latter three changes work because Android never performs any PKI
operations with the certificate. There is no CA certificate chain. The
X.509 certificate file is nothing more than a way to transport an RSA
public key.

avbroot requires the user's key to be RSA 4096. If the original zip had
the same key size, then none of these shrinking methods are needed. If
it contained an RSA 2048 key, then the first two modifications are
usually sufficient. The latter two modifications should only be needed
if the user picked a really long subject value when generating the
certificate.

With these new changes, the OTA patching time will approximately double
on a system with an SSD and modern CPU. This is dominated by the time it
takes to XZ-compress the system partition image. The compression is
already parallelized and scales linearly with the number of cores.
There's likely not much more that can be done to further speed this up.

Finally, these new changes are currently excluded from the e2e tests
because including the system partition in the stripped OTAs would
increase the file size by an order of magnitude. This could potentially
be solved in the future by generating our own small OTAs to use for
testing instead of running against real device OTAs.

Fixes: #225

Signed-off-by: Andrew Gunnerson <[email protected]>
chenxiaolong added a commit that referenced this issue Dec 28, 2023
Closes: #216

Signed-off-by: Andrew Gunnerson <[email protected]>
@chenxiaolong chenxiaolong unpinned this issue Dec 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants