fix: prevent segfault if malicious server sends 1 GB of data through ftpNLST.
[platform/upstream/rpm.git] / rpmdb / rpmdb.h
index e868469..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@*/
@@ -321,8 +322,8 @@ struct _dbiIndex {
     int        dbi_region_init;
     int        dbi_tas_spins;
        /* mpool sub-system parameters */
-    int        dbi_mp_mmapsize;        /*!< (10Mb) */
-    int        dbi_mp_size;    /*!< (128Kb) */
+    int        dbi_mmapsize;   /*!< (10Mb) */
+    int        dbi_cachesize;  /*!< (128Kb) */
        /* lock sub-system parameters */
     unsigned int dbi_lk_max;
     unsigned int dbi_lk_detect;
@@ -340,7 +341,6 @@ struct _dbiIndex {
        /*@modifies fileSystem @*/;
 #endif
        /* dbinfo parameters */
-    int        dbi_cachesize;          /*!< */
     int        dbi_pagesize;           /*!< (fs blksize) */
 /*@unused@*/ /*@null@*/
     void * (*dbi_malloc) (size_t nbytes)
@@ -429,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. */
 };
@@ -555,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
@@ -574,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
@@ -594,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
@@ -613,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
@@ -908,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.
@@ -1005,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.
@@ -1015,8 +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, internalState @*/
+       /*@modifies fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Destroy rpm database iterator.
@@ -1025,8 +1053,8 @@ Header rpmdbNextIterator(/*@null@*/ rpmdbMatchIterator mi)
  */
 /*@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.
@@ -1039,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.
@@ -1054,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.