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

Commit b9596e5f authored by Daniel Sandler's avatar Daniel Sandler Committed by Android (Google) Code Review
Browse files

Merge "Harden against invalid paths." into pi-dev

parents b262dd1b 98267b36
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -282,11 +282,14 @@ public class SliceClientPermissions implements DirtyTracker, Persistable {
        public synchronized void writeTo(XmlSerializer out) throws IOException {
            final int N = mPaths.size();
            for (int i = 0; i < N; i++) {
                final String[] segments = mPaths.valueAt(i);
                if (segments != null) {
                    out.startTag(NAMESPACE, TAG_PATH);
                out.text(encodeSegments(mPaths.valueAt(i)));
                    out.text(encodeSegments(segments));
                    out.endTag(NAMESPACE, TAG_PATH);
                }
            }
        }

        public synchronized void readFrom(XmlPullParser parser)
                throws IOException, XmlPullParserException {
+9 −2
Original line number Diff line number Diff line
@@ -315,7 +315,8 @@ public class SlicePermissionManager implements DirtyTracker {
        return new AtomicFile(new File(mSliceDir, fileName));
    }

    private void handlePersist() {
    @VisibleForTesting
    void handlePersist() {
        synchronized (this) {
            for (Persistable persistable : mDirty) {
                AtomicFile file = getFile(persistable.getFileName());
@@ -335,7 +336,7 @@ public class SlicePermissionManager implements DirtyTracker {

                    out.flush();
                    file.finishWrite(stream);
                } catch (IOException | XmlPullParserException e) {
                } catch (IOException | XmlPullParserException | RuntimeException e) {
                    Slog.w(TAG, "Failed to save access file, restoring backup", e);
                    file.failWrite(stream);
                }
@@ -344,6 +345,12 @@ public class SlicePermissionManager implements DirtyTracker {
        }
    }

    // use addPersistableDirty(); this is just for tests
    @VisibleForTesting
    void addDirtyImmediate(Persistable obj) {
        mDirty.add(obj);
    }

    private void handleRemove(PkgUser pkgUser) {
        getFile(SliceClientPermissions.getFileName(pkgUser)).delete();
        getFile(SliceProviderPermissions.getFileName(pkgUser)).delete();
+31 −1
Original line number Diff line number Diff line
@@ -101,4 +101,34 @@ public class SlicePermissionManagerTest extends UiServiceTestCase {
        assertTrue(FileUtils.deleteContentsAndDir(sliceDir));
    }

    @Test
    public void testInvalid() throws Exception {
        File sliceDir = new File(mContext.getCacheDir(), "slices-test");
        if (!sliceDir.exists()) {
            sliceDir.mkdir();
        }
        SlicePermissionManager permissions = new SlicePermissionManager(mContext,
                TestableLooper.get(this).getLooper(), sliceDir);

        DirtyTracker.Persistable junk = new DirtyTracker.Persistable() {
            @Override
            public String getFileName() {
                return "invalidData";
            }

            @Override
            public void writeTo(XmlSerializer out) throws IOException {
                throw new RuntimeException("this doesn't work");
            }
        };

        // let's put something bad in here
        permissions.addDirtyImmediate(junk);
        // force a persist. if this throws, it would take down system_server
        permissions.handlePersist();

        // Cleanup.
        assertTrue(FileUtils.deleteContentsAndDir(sliceDir));
    }

}