fix: prevent segfault if malicious server sends 1 GB of data through ftpNLST.
[platform/upstream/rpm.git] / rpmdb / rpmdb.h
index a9175df..afb4609 100644 (file)
@@ -8,8 +8,9 @@
  */
 
 #include <assert.h>
-#include <rpmlib.h>
-#include <db.h>
+#include "rpmlib.h"
+#include "rpmsw.h"
+#include "db.h"
 
 /*@-exportlocal@*/
 /*@unchecked@*/
@@ -428,6 +429,10 @@ struct rpmdb_s {
     int                db_ndbi;        /*!< No. of tag indices. */
     dbiIndex * _dbi;           /*!< Tag indices. */
 
+    struct rpmop_s db_getops;
+    struct rpmop_s db_putops;
+    struct rpmop_s db_delops;
+
 /*@refs@*/
     int nrefs;                 /*!< Reference count. */
 };
@@ -554,11 +559,15 @@ int dbiCdup(dbiIndex dbi, DBC * dbcursor, /*@out@*/ DBC ** dbcp,
 /*@unused@*/ static inline
 int dbiDel(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
                unsigned int flags)
-       /*@globals fileSystem @*/
-       /*@modifies *dbcursor, fileSystem @*/
+       /*@globals fileSystem, internalState @*/
+       /*@modifies dbi, *dbcursor, fileSystem, internalState @*/
 {
+    int rc;
     assert(key->data != NULL && key->size > 0);
-    return (dbi->dbi_vec->cdel) (dbi, dbcursor, key, data, flags);
+    (void) rpmswEnter(&dbi->dbi_rpmdb->db_delops, 0);
+    rc = (dbi->dbi_vec->cdel) (dbi, dbcursor, key, data, flags);
+    (void) rpmswExit(&dbi->dbi_rpmdb->db_delops, data->size);
+    return rc;
 }
 
 /** \ingroup dbi
@@ -573,11 +582,15 @@ int dbiDel(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
 /*@unused@*/ static inline
 int dbiGet(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
                unsigned int flags)
-       /*@globals fileSystem @*/
-       /*@modifies *dbcursor, *key, *data, fileSystem @*/
+       /*@globals fileSystem, internalState @*/
+       /*@modifies dbi, *dbcursor, *key, *data, fileSystem, internalState @*/
 {
+    int rc;
     assert((flags == DB_NEXT) || (key->data != NULL && key->size > 0));
-    return (dbi->dbi_vec->cget) (dbi, dbcursor, key, data, flags);
+    (void) rpmswEnter(&dbi->dbi_rpmdb->db_getops, 0);
+    rc = (dbi->dbi_vec->cget) (dbi, dbcursor, key, data, flags);
+    (void) rpmswExit(&dbi->dbi_rpmdb->db_getops, data->size);
+    return rc;
 }
 
 /** \ingroup dbi
@@ -593,11 +606,15 @@ int dbiGet(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
 /*@unused@*/ static inline
 int dbiPget(dbiIndex dbi, /*@null@*/ DBC * dbcursor,
                DBT * key, DBT * pkey, DBT * data, unsigned int flags)
-       /*@globals fileSystem @*/
-       /*@modifies *dbcursor, *key, *pkey, *data, fileSystem @*/
+       /*@globals fileSystem, internalState @*/
+       /*@modifies dbi, *dbcursor, *key, *pkey, *data, fileSystem, internalState @*/
 {
+    int rc;
     assert((flags == DB_NEXT) || (key->data != NULL && key->size > 0));
-    return (dbi->dbi_vec->cpget) (dbi, dbcursor, key, pkey, data, flags);
+    (void) rpmswEnter(&dbi->dbi_rpmdb->db_getops, 0);
+    rc = (dbi->dbi_vec->cpget) (dbi, dbcursor, key, pkey, data, flags);
+    (void) rpmswExit(&dbi->dbi_rpmdb->db_getops, data->size);
+    return rc;
 }
 
 /** \ingroup dbi
@@ -612,11 +629,15 @@ int dbiPget(dbiIndex dbi, /*@null@*/ DBC * dbcursor,
 /*@unused@*/ static inline
 int dbiPut(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data,
                unsigned int flags)
-       /*@globals fileSystem @*/
-       /*@modifies *dbcursor, *key, fileSystem @*/
+       /*@globals fileSystem, internalState @*/
+       /*@modifies dbi, *dbcursor, *key, fileSystem, internalState @*/
 {
+    int rc;
     assert(key->data != NULL && key->size > 0 && data->data != NULL && data->size > 0);
-    return (dbi->dbi_vec->cput) (dbi, dbcursor, key, data, flags);
+    (void) rpmswEnter(&dbi->dbi_rpmdb->db_putops, 0);
+    rc = (dbi->dbi_vec->cput) (dbi, dbcursor, key, data, flags);
+    (void) rpmswExit(&dbi->dbi_rpmdb->db_putops, data->size);
+    return rc;
 }
 
 /** \ingroup dbi
@@ -907,8 +928,8 @@ int rpmdbOpenAll (/*@null@*/ rpmdb db)
  * @return             number of instances
  */
 int rpmdbCountPackages(/*@null@*/ rpmdb db, const char * name)
-       /*@globals rpmGlobalMacroContext, fileSystem @*/
-       /*@modifies db, rpmGlobalMacroContext, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Return header join key for current position of rpm database iterator.
@@ -1004,8 +1025,8 @@ int rpmdbSetHdrChk(/*@null@*/ rpmdbMatchIterator mi, /*@null@*/ rpmts ts,
 /*@only@*/ /*@null@*/
 rpmdbMatchIterator rpmdbInitIterator(/*@null@*/ rpmdb db, rpmTag rpmtag,
                        /*@null@*/ const void * keyp, size_t keylen)
-       /*@globals rpmGlobalMacroContext, fileSystem @*/
-       /*@modifies db, rpmGlobalMacroContext, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Return next package header from iteration.
@@ -1014,16 +1035,16 @@ rpmdbMatchIterator rpmdbInitIterator(/*@null@*/ rpmdb db, rpmTag rpmtag,
  */
 /*@null@*/
 Header rpmdbNextIterator(/*@null@*/ rpmdbMatchIterator mi)
-       /*@globals rpmGlobalMacroContext, fileSystem @*/
-       /*@modifies mi, rpmGlobalMacroContext, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies mi, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Check rpmdb signal handler for trapped signal exit.
  */
 /*@mayexit@*/
 int rpmdbCheckSignals(void)
-       /*@globals fileSystem @*/
-       /*@modifies fileSystem @*/;
+       /*@globals fileSystem, internalState @*/
+       /*@modifies fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Destroy rpm database iterator.
@@ -1032,8 +1053,8 @@ int rpmdbCheckSignals(void)
  */
 /*@null@*/
 rpmdbMatchIterator rpmdbFreeIterator(/*@only@*/ /*@null@*/rpmdbMatchIterator mi)
-       /*@globals rpmGlobalMacroContext, fileSystem @*/
-       /*@modifies mi, rpmGlobalMacroContext, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies mi, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Add package header to rpm database and indices.
@@ -1046,8 +1067,8 @@ rpmdbMatchIterator rpmdbFreeIterator(/*@only@*/ /*@null@*/rpmdbMatchIterator mi)
  */
 int rpmdbAdd(/*@null@*/ rpmdb db, int iid, Header h, /*@null@*/ rpmts ts,
                /*@null@*/ rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, const char ** msg))
-       /*@globals rpmGlobalMacroContext, fileSystem @*/
-       /*@modifies db, h, rpmGlobalMacroContext, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, h, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Remove package header from rpm database and indices.
@@ -1061,8 +1082,8 @@ int rpmdbAdd(/*@null@*/ rpmdb db, int iid, Header h, /*@null@*/ rpmts ts,
 int rpmdbRemove(/*@null@*/ rpmdb db, /*@unused@*/ int rid, unsigned int hdrNum,
                /*@null@*/ rpmts ts,
                /*@null@*/ rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, const char ** msg))
-       /*@globals rpmGlobalMacroContext, fileSystem @*/
-       /*@modifies db, rpmGlobalMacroContext, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Rebuild database indices from package headers.