File: zip_analyzer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (154 lines) | stat: -rw-r--r-- 4,849 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/utility/safe_browsing/zip_analyzer.h"

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <set>

#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/numerics/checked_math.h"
#include "base/rand_util.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/common/safe_browsing/archive_analyzer_results.h"
#include "components/safe_browsing/content/common/file_type_policies.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/proto/csd.pb.h"
#include "third_party/zlib/google/zip_reader.h"

namespace safe_browsing {

namespace {

class WriterDelegate : public zip::FileWriterDelegate {
 public:
  explicit WriterDelegate(base::File* file)
      : zip::FileWriterDelegate(file), has_disk_error_(false) {}
  WriterDelegate(const WriterDelegate&) = delete;
  WriterDelegate& operator=(const WriterDelegate&) = delete;

  ~WriterDelegate() override = default;

  bool PrepareOutput() override {
    bool success = zip::FileWriterDelegate::PrepareOutput();
    has_disk_error_ |= !success;
    return success;
  }
  bool WriteBytes(const char* data, int num_bytes) override {
    bool success = zip::FileWriterDelegate::WriteBytes(data, num_bytes);
    has_disk_error_ |= !success;
    return success;
  }

  bool has_disk_error() const { return has_disk_error_; }

 private:
  bool has_disk_error_;
};

}  // namespace

ZipAnalyzer::ZipAnalyzer() = default;
ZipAnalyzer::~ZipAnalyzer() = default;

void ZipAnalyzer::Init() {
  GetTempFile(
      base::BindOnce(&ZipAnalyzer::OnGetTempFile, weak_factory_.GetWeakPtr()));
}

bool ZipAnalyzer::ResumeExtraction() {
  while (const zip::ZipReader::Entry* const entry = reader_.Next()) {
    // Clear the `temp_file` between extractions.
    if (temp_file_.Seek(base::File::Whence::FROM_BEGIN, 0) != 0) {
      PLOG(WARNING) << "Failed seek";
    }

    // Since this code is expected to run within a utility process, this call
    // will fail on some platforms. We handle this by passing the length
    // into `UpdateResultsForEntry`, which will only consider
    // the appropriate bytes. See crbug.com/1309879 and crbug.com/774762.
    if (!temp_file_.SetLength(0)) {
      PLOG(WARNING) << "Failed truncate";
    }
    WriterDelegate writer(&temp_file_);
    bool extract_success = reader_.ExtractCurrentEntry(
        &writer, std::numeric_limits<uint64_t>::max());

    has_encrypted_ |= entry->is_encrypted;
    has_aes_encrypted_ |= entry->uses_aes_encryption;
    has_disk_error_ |= writer.has_disk_error();

    if (!extract_success && entry->is_encrypted) {
      results()->encryption_info.password_status =
          EncryptionInfo::kKnownIncorrect;
    }

    if (!UpdateResultsForEntry(temp_file_.Duplicate(),
                               GetRootPath().Append(entry->path),
                               writer.file_length(), entry->is_encrypted,
                               entry->is_directory, extract_success)) {
      return false;
    }
  }

  if (has_encrypted_) {
    if (has_aes_encrypted_ && password() && !password()->empty()) {
      results()->encryption_info.password_status = EncryptionInfo::kUnknown;
    } else if (results()->encryption_info.password_status !=
               EncryptionInfo::kKnownIncorrect) {
      results()->encryption_info.password_status =
          EncryptionInfo::kKnownCorrect;
    }
  }

  if (has_disk_error_) {
    results()->analysis_result = ArchiveAnalysisResult::kDiskError;
  } else if (reader_.ok()) {
    results()->analysis_result = ArchiveAnalysisResult::kValid;
  } else {
    results()->analysis_result = ArchiveAnalysisResult::kFailedDuringIteration;
  }

  results()->success = reader_.ok() && !has_disk_error_;
  return true;
}

base::WeakPtr<ArchiveAnalyzer> ZipAnalyzer::GetWeakPtr() {
  return weak_factory_.GetWeakPtr();
}

void ZipAnalyzer::OnGetTempFile(base::File temp_file) {
  if (!temp_file.IsValid()) {
    InitComplete(ArchiveAnalysisResult::kFailedToOpenTempFile);
    return;
  }

  if (!reader_.OpenFromPlatformFile(GetArchiveFile().GetPlatformFile())) {
    InitComplete(ArchiveAnalysisResult::kUnknown);
    return;
  }

  bool too_big_to_unpack =
      base::checked_cast<uint64_t>(GetArchiveFile().GetLength()) >
      FileTypePolicies::GetInstance()->GetMaxFileSizeToAnalyze("zip");
  if (too_big_to_unpack) {
    InitComplete(ArchiveAnalysisResult::kTooLarge);
    return;
  }
  temp_file_ = std::move(temp_file);

  if (password().has_value()) {
    reader_.SetPassword(*password());
  }

  InitComplete(ArchiveAnalysisResult::kValid);
}

}  // namespace safe_browsing