mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-02 20:21:59 -05:00
fix(native): terminals use the ioctls Android allows on an app's pseudo-terminal (#3585)
* fix(native): terminals use the ioctls Android allows on an app's pseudo-terminal * fix(native): leave a terminal request alone when its register is read before the high half
This commit is contained in:
1 parent
f3f410059d
commit
3bb22dc92b
2 files changed
+73
No files matched your search
@@ -38,6 +38,11 @@ object ElfSeccompPatch {
|
||||
val end = minOf(data.size, buffer.getLong(at + 8).toInt() + buffer.getLong(at + 32).toInt()) and 3.inv()
|
||||
var pos = start
|
||||
while (pos + 4 <= end) {
|
||||
if (patchTermiosRequest(buffer, pos, end)) {
|
||||
patched++
|
||||
pos += 4
|
||||
continue
|
||||
}
|
||||
val replacement = when (buffer.getInt(pos)) {
|
||||
MOV_X8_SET_ROBUST_LIST -> NOP
|
||||
MOV_X8_CLONE3, MOV_X8_RSEQ -> MOV_X0_ENOSYS
|
||||
@@ -65,6 +70,40 @@ object ElfSeccompPatch {
|
||||
return patched
|
||||
}
|
||||
|
||||
/**
|
||||
* glibc 2.42 and later read and set a terminal with TCGETS2 and TCSETS2/W2/F2, which Android's
|
||||
* SELinux policy refuses on an app's pseudo-terminal slaves (EACCES): isatty() was false in every
|
||||
* Konsole, ssh and tmux session, so bash had no prompt or job control and stdout was not line
|
||||
* buffered. TCGETS and TCSETS* carry the same struct without its two speed fields, which glibc
|
||||
* then takes from c_cflag. A site is `movz xN, #lo` then `movk xN, #hi, lsl #16`; the movz gets
|
||||
* the old request and the movk becomes a no-op.
|
||||
*/
|
||||
private fun patchTermiosRequest(buffer: ByteBuffer, pos: Int, end: Int): Boolean {
|
||||
val movz = buffer.getInt(pos)
|
||||
if ((movz and 0x7fe00000) != 0x52800000) return false
|
||||
val low = (movz ushr 5) and 0xffff
|
||||
if (low !in 0x542a..0x542d) return false
|
||||
val register = movz and 0x1f
|
||||
var next = pos + 4
|
||||
while (next < minOf(end, pos + 4 + 4 * 4)) {
|
||||
val movk = buffer.getInt(next)
|
||||
if ((movk and 0x7fe00000) == 0x72a00000 && (movk and 0x1f) == register) {
|
||||
val request = when ((movk ushr 5) and 0xffff) {
|
||||
0x802c -> if (low == 0x542a) 0x5401 else return false
|
||||
0x402c -> if (low != 0x542a) 0x5402 + (low - 0x542b) else return false
|
||||
else -> return false
|
||||
}
|
||||
buffer.putInt(pos, (movz and 0x1f.inv() and (0xffff shl 5).inv()) or (request shl 5) or register)
|
||||
buffer.putInt(next, NOP)
|
||||
return true
|
||||
}
|
||||
// Anything between that might read or write the register would see the old low half
|
||||
if (intArrayOf(0, 5, 10, 16).any { (movk ushr it) and 0x1f == register }) return false
|
||||
next += 4
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/** Patches the file behind [path] (symlinks followed) if it still has unpatched call sites. */
|
||||
fun patchFile(path: File): Int {
|
||||
val target = path.canonicalFile
|
||||
|
||||
@@ -44,6 +44,40 @@ class ElfSeccompPatchTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun terminalRequestsUseTheOnesAndroidAllowsOnAPseudoTerminal() {
|
||||
// glibc 2.43's tcsetattr: TCSETS2 into x0, TCGETS2 into x1, with an unrelated instruction between
|
||||
val data = elfWith(
|
||||
0xd28a8560.toInt(), 0x910123e2.toInt(), 0xf2a80580.toInt(),
|
||||
0xd28a8541.toInt(), 0x93407ea0.toInt(), 0xf2b00581.toInt(),
|
||||
0x528a85a3.toInt(), 0x72a80583.toInt(),
|
||||
)
|
||||
assertEquals(3, ElfSeccompPatch.patch(data))
|
||||
val b = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN)
|
||||
val nop = 0xd503201f.toInt()
|
||||
assertEquals(0xd28a8040.toInt(), b.getInt(0x100)) // mov x0, #0x5402 (TCSETS)
|
||||
assertEquals(nop, b.getInt(0x108))
|
||||
assertEquals(0xd28a8021.toInt(), b.getInt(0x10c)) // mov x1, #0x5401 (TCGETS)
|
||||
assertEquals(nop, b.getInt(0x114))
|
||||
assertEquals(0x528a8083.toInt(), b.getInt(0x118)) // mov w3, #0x5404 (TCSETSF)
|
||||
assertEquals(nop, b.getInt(0x11c))
|
||||
assertEquals(0, ElfSeccompPatch.patch(data))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aLookalikeConstantIsLeftAlone() {
|
||||
// 0x542a with another high half, and one whose high half goes to another register
|
||||
val data = elfWith(0xd28a8541.toInt(), 0xf2a00021.toInt(), 0xd28a8541.toInt(), 0xf2b00582.toInt())
|
||||
assertEquals(0, ElfSeccompPatch.patch(data))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aSiteWhoseRegisterIsReadBeforeTheHighHalfIsLeftAlone() {
|
||||
// movz x1, #0x542a; add x2, x1, #1; movk x1, #0x802c, lsl #16; movk x2, #0x402c, lsl #16
|
||||
val data = elfWith(0xd28a8541.toInt(), 0x91000422.toInt(), 0xf2b00581.toInt(), 0xf2a80582.toInt())
|
||||
assertEquals(0, ElfSeccompPatch.patch(data))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun otherSystemCallsAreLeftAlone() {
|
||||
val svc = 0xd4000001.toInt()
|
||||
|
||||
Reference in new issue
Block a user