fix: prevent segfault if malicious server sends 1 GB of data through ftpNLST.
[platform/upstream/rpm.git] / rpmdb / rpmdb.h
index 86a9329..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@*/
@@ -294,7 +295,6 @@ struct _dbiIndex {
     int        dbi_api;                /*!< Berkeley API type */
 
     int        dbi_verify_on_close;
-    int        dbi_tear_down;          /*!< tear down dbenv on close */
     int        dbi_use_dbenv;          /*!< use db environment? */
     int        dbi_permit_dups;        /*!< permit duplicate entries? */
     int        dbi_no_fsync;           /*!< no-op fsync for db */
@@ -322,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;
@@ -341,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)
@@ -423,12 +422,17 @@ struct rpmdb_s {
 /*@only@*/ /*@null@*/
     unsigned char * db_bits;   /*!< package instance bit mask. */
     int                db_nbits;       /*!< no. of bits in mask. */
+    rpmdb      db_next;
     int                db_opens;
 /*@only@*/ /*@null@*/
     void *     db_dbenv;       /*!< Berkeley DB_ENV handle. */
     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. */
 };
@@ -491,7 +495,8 @@ extern const char *const prDbiOpenFlags(int dbflags, int print_dbenv_flags)
  */
 /*@only@*/ /*@null@*/ dbiIndex dbiOpen(/*@null@*/ rpmdb db, rpmTag rpmtag,
                unsigned int flags)
-       /*@modifies db @*/;
+       /*@globals rpmGlobalMacroContext, errno @*/
+       /*@modifies db, rpmGlobalMacroContext, errno @*/;
 
 /*@-globuse -mustmod @*/ /* FIX: vector annotations */
 /** \ingroup dbi
@@ -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
@@ -839,8 +860,8 @@ rpmdb XrpmdbLink (rpmdb db, const char * msg,
  */
 int rpmdbOpen (/*@null@*/ const char * prefix, /*@null@*/ /*@out@*/ rpmdb * dbp,
                int mode, int perms)
-       /*@globals fileSystem @*/
-       /*@modifies *dbp, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies *dbp, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Initialize database.
@@ -849,8 +870,8 @@ int rpmdbOpen (/*@null@*/ const char * prefix, /*@null@*/ /*@out@*/ rpmdb * dbp,
  * @return             0 on success
  */
 int rpmdbInit(/*@null@*/ const char * prefix, int perms)
-       /*@globals fileSystem @*/
-       /*@modifies fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Verify database components.
@@ -858,8 +879,8 @@ int rpmdbInit(/*@null@*/ const char * prefix, int perms)
  * @return             0 on success
  */
 int rpmdbVerify(/*@null@*/ const char * prefix)
-       /*@globals fileSystem @*/
-       /*@modifies fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /**
  * Close a single database index.
@@ -896,7 +917,8 @@ int rpmdbSync (/*@null@*/ rpmdb db)
  */
 /*@-exportlocal@*/
 int rpmdbOpenAll (/*@null@*/ rpmdb db)
-       /*@modifies db @*/;
+       /*@globals rpmGlobalMacroContext @*/
+       /*@modifies db, rpmGlobalMacroContext @*/;
 /*@=exportlocal@*/
 
 /** \ingroup rpmdb
@@ -906,8 +928,8 @@ int rpmdbOpenAll (/*@null@*/ rpmdb db)
  * @return             number of instances
  */
 int rpmdbCountPackages(/*@null@*/ rpmdb db, const char * name)
-       /*@globals fileSystem @*/
-       /*@modifies db, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Return header join key for current position of rpm database iterator.
@@ -959,7 +981,8 @@ int rpmdbPruneIterator(/*@null@*/ rpmdbMatchIterator mi,
  */
 int rpmdbSetIteratorRE(/*@null@*/ rpmdbMatchIterator mi, rpmTag tag,
                rpmMireMode mode, /*@null@*/ const char * pattern)
-       /*@modifies mi, mode @*/;
+       /*@globals rpmGlobalMacroContext @*/
+       /*@modifies mi, mode, rpmGlobalMacroContext @*/;
 
 /** \ingroup rpmdb
  * Prepare iterator for lazy writes.
@@ -1002,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 fileSystem @*/
-       /*@modifies db, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Return next package header from iteration.
@@ -1012,8 +1035,16 @@ rpmdbMatchIterator rpmdbInitIterator(/*@null@*/ rpmdb db, rpmTag rpmtag,
  */
 /*@null@*/
 Header rpmdbNextIterator(/*@null@*/ rpmdbMatchIterator mi)
-       /*@globals fileSystem @*/
-       /*@modifies mi, 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.
@@ -1022,8 +1053,8 @@ Header rpmdbNextIterator(/*@null@*/ rpmdbMatchIterator mi)
  */
 /*@null@*/
 rpmdbMatchIterator rpmdbFreeIterator(/*@only@*/ /*@null@*/rpmdbMatchIterator mi)
-       /*@globals fileSystem @*/
-       /*@modifies mi, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies mi, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Add package header to rpm database and indices.
@@ -1036,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 fileSystem @*/
-       /*@modifies db, h, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, h, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Remove package header from rpm database and indices.
@@ -1051,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 fileSystem @*/
-       /*@modifies db, fileSystem @*/;
+       /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
+       /*@modifies db, rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmdb
  * Rebuild database indices from package headers.