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

Commit 0ed9b68a authored by Yi Jin's avatar Yi Jin
Browse files

Implement PII stripper in incidentd, part 1

1. automatically parse privacy options and generate lookup table
2. create FdBuffer iterator API in order to remove dependency on Reporter.h

Bug: 64687253
Test: Unit test for iterator API, and manually tested lookup table
Change-Id: I1ea376a4481fc4afc7bdf447936f767b63690fd3
parent 6e250517
Loading
Loading
Loading
Loading
+28 −1
Original line number Diff line number Diff line
@@ -241,6 +241,7 @@ FdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeou
size_t
FdBuffer::size()
{
    if (mBuffers.empty()) return 0;
    return ((mBuffers.size() - 1) * BUFFER_SIZE) + mCurrentWritten;
}

@@ -255,4 +256,30 @@ FdBuffer::write(ReportRequestSet* reporter)
    return NO_ERROR;
}

FdBuffer::iterator
FdBuffer::end()
{
    if (mBuffers.empty() || mCurrentWritten < 0) return begin();
    if (mCurrentWritten == BUFFER_SIZE)
        // FdBuffer doesn't allocate another buf since no more bytes to read.
        return FdBuffer::iterator(*this, mBuffers.size(), 0);
    return FdBuffer::iterator(*this, mBuffers.size() - 1, mCurrentWritten);
}

FdBuffer::iterator&
FdBuffer::iterator::operator+(size_t offset)
{
    size_t newOffset = mOffset + offset;
    while (newOffset >= BUFFER_SIZE) {
        mIndex++;
        newOffset -= BUFFER_SIZE;
    }
    mOffset = newOffset;
    return *this;
}

size_t
FdBuffer::iterator::bytesRead()
{
    return mIndex * BUFFER_SIZE + mOffset;
}
+33 −2
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@

#include <utils/Errors.h>

#include <set>
#include <vector>

using namespace android;
@@ -74,7 +73,8 @@ public:
    size_t size();

    /**
     * Write the data that we recorded to the fd given.
     * [Deprecated] Write the data that we recorded to the fd given.
     * TODO: remove it once the iterator api is working
     */
    status_t write(ReportRequestSet* requests);

@@ -83,6 +83,37 @@ public:
     */
    int64_t durationMs() { return mFinishTime - mStartTime; }

    /**
     * Read data stored in FdBuffer
     */
    class iterator;
    friend class iterator;
    class iterator : public std::iterator<std::random_access_iterator_tag, uint8_t> {
    private:
        FdBuffer& mFdBuffer;
        size_t mIndex;
        size_t mOffset;
    public:
        explicit iterator(FdBuffer& buffer, ssize_t index, ssize_t offset)
                : mFdBuffer(buffer), mIndex(index), mOffset(offset) {}
        iterator& operator=(iterator& other) { return other; }
        iterator& operator+(size_t offset); // this is implemented in .cpp
        iterator& operator+=(size_t offset) { return *this + offset; }
        iterator& operator++() { return *this + 1; }
        iterator operator++(int) { return *this + 1; }
        bool operator==(iterator other) const {
            return mIndex == other.mIndex && mOffset == other.mOffset;
        }
        bool operator!=(iterator other) const { return !(*this == other); }
        reference operator*() const { return mFdBuffer.mBuffers[mIndex][mOffset]; }

        // random access could make the iterator out of bound
        size_t bytesRead();
        bool outOfBound() { return bytesRead() > mFdBuffer.size(); };
    };
    iterator begin() { return iterator(*this, 0, 0); }
    iterator end();

private:
    vector<uint8_t*> mBuffers;
    int64_t mStartTime;
+1 −4
Original line number Diff line number Diff line
@@ -22,9 +22,6 @@
#include <private/android_filesystem_config.h>
#include <binder/IServiceManager.h>
#include <mutex>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wait.h>
#include <unistd.h>

@@ -37,7 +34,7 @@ const char* INCIDENT_HELPER = "/system/bin/incident_helper";
static pid_t
forkAndExecuteIncidentHelper(const int id, const char* name, Fpipe& p2cPipe, Fpipe& c2pPipe)
{
    const char* ihArgs[] { INCIDENT_HELPER, "-s", to_string(id).c_str(), NULL };
    const char* ihArgs[] { INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL };

    // fork used in multithreaded environment, avoid adding unnecessary code in child process
    pid_t pid = fork();
+1 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#include "protobuf.h"


uint8_t* 
write_raw_varint(uint8_t* buf, uint32_t val)
{
+38 −0
Original line number Diff line number Diff line
@@ -21,8 +21,46 @@

/**
 * This is the mapping of section IDs to the commands that are run to get those commands.
 * The section IDs are guaranteed in ascending order
 */
extern const Section* SECTION_LIST[];

/*
 * In order not to use libprotobuf-cpp-full nor libplatformprotos in incidentd
 * privacy options's data structure are explicityly redefined in this file.
 */

// DESTINATION enum
extern const uint8_t DEST_LOCAL;
extern const uint8_t DEST_EXPLICIT;
extern const uint8_t DEST_AUTOMATIC;

// This is the default value of DEST enum
// field with this value doesn't generate Privacy to save too much generated code
extern const uint8_t DEST_DEFAULT_VALUE;

// type of the field, identitical to protobuf definition
extern const uint8_t TYPE_STRING;
extern const uint8_t TYPE_MESSAGE;

struct Privacy {
    int field_id;
    uint8_t type;

    // the following two fields are identitical to
    // frameworks/base/libs/incident/proto/android/privacy.proto
    uint8_t dest;
    const char** patterns;

    // ignore parent's privacy flags if children are set, NULL-terminated
    const Privacy** children;
};

/**
 * This is the mapping of section IDs to each section's privacy policy.
 * The section IDs are guaranteed in ascending order
 */
extern const Privacy* PRIVACY_POLICY_LIST[];

#endif // SECTION_LIST_H
Loading