]> git.saurik.com Git - apt.git/commitdiff
try not to call memcpy with length 0 in hash calculations
authorDavid Kalnischkies <david@kalnischkies.de>
Wed, 31 Aug 2016 08:11:07 +0000 (10:11 +0200)
committerDavid Kalnischkies <david@kalnischkies.de>
Thu, 1 Sep 2016 14:13:14 +0000 (16:13 +0200)
memcpy is marked as nonnull for its input, but ignores the input anyhow
if the declared length is zero. Our SHA2 implementations do this as
well, it was "just" MD5 and SHA1 missing, so we add the length check
here as well as along the callstack as it is really pointless to do all
these method calls for "nothing".

Reported-By: gcc -fsanitize=undefined
apt-pkg/contrib/hashes.cc
apt-pkg/contrib/hashes.h
apt-pkg/contrib/hashsum_template.h
apt-pkg/contrib/md5.cc
apt-pkg/contrib/md5.h
apt-pkg/contrib/sha1.cc
apt-pkg/contrib/sha1.h
apt-pkg/contrib/sha2.h
methods/rred.cc

index 755ad20351443d321a6372ad614f2e1e2218e0fe..662c2bf8ba64b58a432ec17f721fe079282f24f1 100644 (file)
@@ -312,6 +312,8 @@ public:
 // Hashes::Add* - Add the contents of data or FD                       /*{{{*/
 bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size)
 {
+   if (Size == 0)
+      return true;
    bool Res = true;
 APT_IGNORE_DEPRECATED_PUSH
    if ((d->CalcHashes & MD5SUM) == MD5SUM)
index 9bfc32c54aeb7c4bb05572dad2c7f884cddef234..1fe0afc0099cf3147bce8235e4b7e93406d25db8 100644 (file)
@@ -195,11 +195,11 @@ class Hashes
 
    static const int UntilEOF = 0;
 
-   bool Add(const unsigned char * const Data, unsigned long long const Size);
-   APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes);
-   inline bool Add(const char * const Data)
+   bool Add(const unsigned char * const Data, unsigned long long const Size) APT_NONNULL(2);
+   APT_DEPRECATED_MSG("Construct accordingly instead of choosing hashes while adding") bool Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes) APT_NONNULL(2);
+   inline bool Add(const char * const Data) APT_NONNULL(2)
    {return Add((unsigned char const * const)Data,strlen(Data));};
-   inline bool Add(const unsigned char * const Beg,const unsigned char * const End)
+   inline bool Add(const unsigned char * const Beg,const unsigned char * const End) APT_NONNULL(2,3)
    {return Add(Beg,End-Beg);};
 
    enum SupportedHashes { MD5SUM = (1 << 0), SHA1SUM = (1 << 1), SHA256SUM = (1 << 2),
index 4000f230db38c16f30e2f55bea7d052a513119f3..e5032d02fd6f7b4f2e284cc65e801c9f030fb714 100644 (file)
@@ -122,18 +122,18 @@ class HashSumValue
 class SummationImplementation
 {
    public:
-   virtual bool Add(const unsigned char *inbuf, unsigned long long inlen) = 0;
-   inline bool Add(const char *inbuf, unsigned long long const inlen)
+   virtual bool Add(const unsigned char *inbuf, unsigned long long inlen) APT_NONNULL(2) = 0;
+   inline bool Add(const char *inbuf, unsigned long long const inlen) APT_NONNULL(2)
    { return Add((const unsigned char *)inbuf, inlen); }
 
-   inline bool Add(const unsigned char *Data)
+   inline bool Add(const unsigned char *Data) APT_NONNULL(2)
    { return Add(Data, strlen((const char *)Data)); }
-   inline bool Add(const char *Data)
+   inline bool Add(const char *Data) APT_NONNULL(2)
    { return Add((const unsigned char *)Data, strlen(Data)); }
 
-   inline bool Add(const unsigned char *Beg, const unsigned char *End)
+   inline bool Add(const unsigned char *Beg, const unsigned char *End) APT_NONNULL(2,3)
    { return Add(Beg, End - Beg); }
-   inline bool Add(const char *Beg, const char *End)
+   inline bool Add(const char *Beg, const char *End) APT_NONNULL(2,3)
    { return Add((const unsigned char *)Beg, End - Beg); }
 
    bool AddFD(int Fd, unsigned long long Size = 0);
index b487a96f9bb30ca289be03f6f0f073b7e6f542f5..ff7868fe20919403cf7574b90dd53012b4c32ff3 100644 (file)
@@ -187,6 +187,8 @@ bool MD5Summation::Add(const unsigned char *data,unsigned long long len)
 {
    if (Done == true)
       return false;
+   if (len == 0)
+      return true;
 
    uint32_t *buf = (uint32_t *)Buf;
    uint32_t *bytes = (uint32_t *)Bytes;
index a16ea4d2d673dd09a73a567b2478b669548552ab..a286f092aa0b6bf1e264b9e541657249bc7a9a85 100644 (file)
@@ -48,7 +48,7 @@ class MD5Summation : public SummationImplementation
 
    public:
 
-   bool Add(const unsigned char *inbuf, unsigned long long inlen) APT_OVERRIDE;
+   bool Add(const unsigned char *inbuf, unsigned long long inlen) APT_OVERRIDE APT_NONNULL(2);
    using SummationImplementation::Add;
 
    MD5SumValue Result();
index bf6bc6cb63ca2e5e21524a8d83a25e82bb88e5e0..298a7799bda9e5c23713958ecf5517f0f079d25b 100644 (file)
@@ -243,6 +243,8 @@ bool SHA1Summation::Add(const unsigned char *data,unsigned long long len)
 {
    if (Done)
       return false;
+   if (len == 0)
+      return true;
 
    uint32_t *state = (uint32_t *)State;
    uint32_t *count = (uint32_t *)Count;
index 1c5cb5aa17e6bd230e56ecf3413e272677fdace7..3387c1cfd203e1858a60bda5a3419777f4b2a768 100644 (file)
@@ -37,7 +37,7 @@ class SHA1Summation : public SummationImplementation
    bool Done;
    
    public:
-   bool Add(const unsigned char *inbuf, unsigned long long inlen) APT_OVERRIDE;
+   bool Add(const unsigned char *inbuf, unsigned long long inlen) APT_OVERRIDE APT_NONNULL(2);
    using SummationImplementation::Add;
 
    SHA1SumValue Result();
index 8b4bdd439b1c80aef55b7a2799aefdd748ce1713..164840d3b52ff5a0063c0c1900564629bd2b677b 100644 (file)
@@ -34,7 +34,7 @@ class SHA2SummationBase : public SummationImplementation
  protected:
    bool Done;
  public:
-   bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE = 0;
+   bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE APT_NONNULL(2) = 0;
 
    void Result();
 };
@@ -45,7 +45,7 @@ class SHA256Summation : public SHA2SummationBase
    unsigned char Sum[32];
 
    public:
-   bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE
+   bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE APT_NONNULL(2)
    {
       if (Done) 
          return false;
@@ -78,7 +78,7 @@ class SHA512Summation : public SHA2SummationBase
    unsigned char Sum[64];
 
    public:
-   bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE
+   bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE APT_NONNULL(2)
    {
       if (Done) 
          return false;
index 0c641ad826e65724f18a7aa8c4dd7bc8d1f29cac..958933a07b50631ebbdd27d2ebce93bf896885b8 100644 (file)
@@ -335,7 +335,7 @@ class Patch {
    FileChanges filechanges;
    MemBlock add_text;
 
-   static bool retry_fwrite(char *b, size_t l, FileFd &f, Hashes * const start_hash, Hashes * const end_hash = nullptr)
+   static bool retry_fwrite(char *b, size_t l, FileFd &f, Hashes * const start_hash, Hashes * const end_hash = nullptr) APT_NONNULL(1)
    {
       if (f.Write(b, l) == false)
         return false;
@@ -385,8 +385,8 @@ class Patch {
       }
    }
 
-   static void dump_mem(FileFd &o, char *p, size_t s, Hashes *hash) {
-      retry_fwrite(p, s, o, hash);
+   static void dump_mem(FileFd &o, char *p, size_t s, Hashes *hash) APT_NONNULL(2) {
+      retry_fwrite(p, s, o, nullptr, hash);
    }
 
    public:
@@ -538,7 +538,8 @@ class Patch {
       for (ch = filechanges.begin(); ch != filechanges.end(); ++ch) {
         dump_lines(out, in, ch->offset, start_hash, end_hash);
         skip_lines(in, ch->del_cnt, start_hash);
-        dump_mem(out, ch->add, ch->add_len, end_hash);
+        if (ch->add_len != 0)
+           dump_mem(out, ch->add, ch->add_len, end_hash);
       }
       dump_rest(out, in, start_hash, end_hash);
       out.Flush();