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

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

Implement Pii Stripper Part 3

The incident request args sets privacy spec. Strip action is optimized
to run once for each type of spec and ready for flush multiple times.
Incident command is updated to take -p option to specify privacy spec.

Bug: 64687253
Test: unit tests written, manually run incident command to test as well
Change-Id: I6753df117f76dc1a5f4d2152baa3fbbf56b490e4
parent 99c248fe
Loading
Loading
Loading
Loading
+18 −3
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include <binder/IServiceManager.h>
#include <utils/Looper.h>

#include <cstring>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
@@ -143,6 +144,16 @@ find_section(const char* name)
    return NULL;
}

// ================================================================================
static int
get_dest(const char* arg)
{
    if (strcmp(arg, "LOCAL") == 0) return 0;
    if (strcmp(arg, "EXPLICIT") == 0) return 1;
    if (strcmp(arg, "AUTOMATIC") == 0) return 2;
    return -1; // return the default value
}

// ================================================================================
static void
usage(FILE* out)
@@ -155,6 +166,7 @@ usage(FILE* out)
    fprintf(out, "  -b           (default) print the report to stdout (in proto format)\n");
    fprintf(out, "  -d           send the report into dropbox\n");
    fprintf(out, "  -l           list available sections\n");
    fprintf(out, "  -p           privacy spec, LOCAL, EXPLICIT or AUTOMATIC\n");
    fprintf(out, "\n");
    fprintf(out, "  SECTION     the field numbers of the incident report fields to include\n");
    fprintf(out, "\n");
@@ -166,10 +178,11 @@ main(int argc, char** argv)
    Status status;
    IncidentReportArgs args;
    enum { DEST_DROPBOX, DEST_STDOUT } destination = DEST_STDOUT;
    int dest = -1; // default

    // Parse the args
    int opt;
    while ((opt = getopt(argc, argv, "bhdl")) != -1) {
    while ((opt = getopt(argc, argv, "bhdlp:")) != -1) {
        switch (opt) {
            case 'h':
                usage(stdout);
@@ -183,6 +196,9 @@ main(int argc, char** argv)
            case 'd':
                destination = DEST_DROPBOX;
                break;
            case 'p':
                dest = get_dest(optarg);
                break;
            default:
                usage(stderr);
                return 1;
@@ -210,8 +226,7 @@ main(int argc, char** argv)
            }
        }
    }


    args.setDest(dest);

    // Start the thread pool.
    sp<ProcessState> ps(ProcessState::self());
+2 −2
Original line number Diff line number Diff line
@@ -70,7 +70,6 @@ write_field_or_skip(FdBuffer::iterator &iterator, vector<uint8_t> &buf, uint8_t
    if (skip) {
        iterator += bytesToWrite;
    } else {
        buf.reserve(bytesToWrite);
        for (size_t i=0; i<bytesToWrite; i++) {
            buf.push_back(*iterator);
            iterator++;
@@ -193,3 +192,4 @@ EncodedBuffer::flush(int fd)
    }
    return NO_ERROR;
}
+59 −0
Original line number Diff line number Diff line
@@ -258,6 +258,12 @@ FdBuffer::flush(int fd) const
    return write_all(fd, mBuffers[i], mCurrentWritten);
}

FdBuffer::iterator
FdBuffer::begin() const
{
    return iterator(*this, 0, 0);
}

FdBuffer::iterator
FdBuffer::end() const
{
@@ -268,6 +274,17 @@ FdBuffer::end() const
    return FdBuffer::iterator(*this, mBuffers.size() - 1, mCurrentWritten);
}

// ===============================================================================
FdBuffer::iterator::iterator(const FdBuffer& buffer, ssize_t index, ssize_t offset)
        : mFdBuffer(buffer),
          mIndex(index),
          mOffset(offset)
{
}

FdBuffer::iterator&
FdBuffer::iterator::operator=(iterator& other) const { return other; }

FdBuffer::iterator&
FdBuffer::iterator::operator+(size_t offset)
{
@@ -280,8 +297,50 @@ FdBuffer::iterator::operator+(size_t offset)
    return *this;
}

FdBuffer::iterator&
FdBuffer::iterator::operator+=(size_t offset) { return *this + offset; }

FdBuffer::iterator&
FdBuffer::iterator::operator++() { return *this + 1; }

FdBuffer::iterator
FdBuffer::iterator::operator++(int) { return *this + 1; }

bool
FdBuffer::iterator::operator==(iterator other) const
{
    return mIndex == other.mIndex && mOffset == other.mOffset;
}

bool
FdBuffer::iterator::operator!=(iterator other) const { return !(*this == other); }

int
FdBuffer::iterator::operator-(iterator other) const
{
    return (int)bytesRead() - (int)other.bytesRead();
}

FdBuffer::iterator::reference
FdBuffer::iterator::operator*() const
{
    return mFdBuffer.mBuffers[mIndex][mOffset];
}

FdBuffer::iterator
FdBuffer::iterator::snapshot() const
{
    return FdBuffer::iterator(mFdBuffer, mIndex, mOffset);
}

size_t
FdBuffer::iterator::bytesRead() const
{
    return mIndex * BUFFER_SIZE + mOffset;
}

bool
FdBuffer::iterator::outOfBound() const
{
    return bytesRead() > mFdBuffer.size();
}
+13 −16
Original line number Diff line number Diff line
@@ -87,32 +87,29 @@ public:
    friend class iterator;
    class iterator : public std::iterator<std::random_access_iterator_tag, uint8_t> {
    public:
        iterator(const FdBuffer& buffer, ssize_t index, ssize_t offset)
                : mFdBuffer(buffer), mIndex(index), mOffset(offset) {}
        iterator& operator=(iterator& other) const { 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); }
        int operator-(iterator other) const { return (int)bytesRead() - (int)other.bytesRead(); }
        reference operator*() const { return mFdBuffer.mBuffers[mIndex][mOffset]; }
        iterator(const FdBuffer& buffer, ssize_t index, ssize_t offset);
        iterator& operator=(iterator& other) const;
        iterator& operator+(size_t offset);
        iterator& operator+=(size_t offset);
        iterator& operator++();
        iterator operator++(int);
        bool operator==(iterator other) const;
        bool operator!=(iterator other) const;
        int operator-(iterator other) const;
        reference operator*() const;

        // return the snapshot of the current iterator
        iterator snapshot() const { return iterator(mFdBuffer, mIndex, mOffset); }
        iterator snapshot() const;
        // how many bytes are read
        size_t bytesRead() const;
        // random access could make the iterator out of bound
        bool outOfBound() const { return bytesRead() > mFdBuffer.size(); }
        bool outOfBound() const;
    private:
        const FdBuffer& mFdBuffer;
        size_t mIndex;
        size_t mOffset;
    };
    iterator begin() const { return iterator(*this, 0, 0); }
    iterator begin() const;
    iterator end() const;

private:
+11 −0
Original line number Diff line number Diff line
@@ -87,6 +87,12 @@ static bool allowDest(const uint8_t dest, const uint8_t policy)
    }
}

bool
PrivacySpec::operator<(const PrivacySpec& other) const
{
  return dest < other.dest;
}

bool
PrivacySpec::CheckPremission(const Privacy* privacy) const
{
@@ -97,4 +103,9 @@ PrivacySpec::CheckPremission(const Privacy* privacy) const
bool
PrivacySpec::RequireAll() const { return dest == DEST_LOCAL; }

PrivacySpec new_spec_from_args(int dest) {
  if (dest < 0) return PrivacySpec();
  return PrivacySpec(dest);
}

PrivacySpec get_default_dropbox_spec() { return PrivacySpec(DEST_AUTOMATIC); }
 No newline at end of file
Loading