bound deserialize() recursion

This commit is contained in:
Michael Davidsaver committed 2026-08-09 17:31:03 -07:00
1 parent 777c921535
commit abc95982ed
7 files changed
+70 -2

No files matched your search

+21 -1
View File
@@ -28,6 +28,8 @@
#include <pv/thread.h>
#include <pv/pvData.h>
#include "factoryPvt.h"
using std::tr1::static_pointer_cast;
using std::size_t;
using std::string;
@@ -36,7 +38,6 @@ namespace epics { namespace pvData {
size_t Field::num_instances;
struct Field::Helper {
static unsigned hash(Field *fld) {
std::ostringstream key;
@@ -1476,6 +1477,7 @@ static int decodeScalar(int8 code)
FieldConstPtr FieldCreate::deserialize(ByteBuffer* buffer, DeserializableControl* control) const
{
RecurseGuard RG;
control->ensureData(1);
int8 code = buffer->getByte();
if (code == -1)
@@ -1604,6 +1606,7 @@ FieldConstPtr FieldCreate::deserialize(ByteBuffer* buffer, DeserializableControl
namespace detail {
struct field_factory {
FieldCreatePtr fieldCreate;
epicsThreadPrivate<unsigned> deserDepth;
field_factory() :fieldCreate(new FieldCreate()) {
registerRefCounter("Field", &Field::num_instances);
registerRefCounter("Thread", &Thread::num_instances);
@@ -1614,6 +1617,23 @@ struct field_factory {
static detail::field_factory* field_factory_s;
static epicsThreadOnceId field_factory_once = EPICS_THREAD_ONCE_INIT;
#if defined(__rtems__) || defined(vxWorks)
// lower limit on embedded targets
const unsigned maxDepth = 10;
#else
const unsigned maxDepth = 20;
#endif
RecurseGuard::RecurseGuard() {
size_t current = (size_t)field_factory_s->deserDepth.get();
if(current>=maxDepth)
throw std::logic_error("FieldCreateFactory recursion depth exceeded");
field_factory_s->deserDepth.set((unsigned*)(current+1));
}
RecurseGuard::~RecurseGuard() {
size_t current = (size_t)field_factory_s->deserDepth.get();
field_factory_s->deserDepth.set((unsigned*)(current-1));
}
static void field_factory_init(void*)
{
try {
+3
View File
@@ -18,6 +18,8 @@
#include <pv/factory.h>
#include <pv/bitSet.h>
#include "factoryPvt.h"
using std::tr1::static_pointer_cast;
using std::size_t;
using std::string;
@@ -224,6 +226,7 @@ void PVStructure::serialize(ByteBuffer *pbuffer,
void PVStructure::deserialize(ByteBuffer *pbuffer,
DeserializableControl *pcontrol) {
RecurseGuard G;
size_t fieldsSize = pvFields.size();
for(size_t i = 0; i<fieldsSize; i++)
pvFields[i]->deserialize(pbuffer, pcontrol);
+3
View File
@@ -16,6 +16,8 @@
#include <pv/factory.h>
#include <pv/serializeHelper.h>
#include "factoryPvt.h"
using std::tr1::static_pointer_cast;
using std::size_t;
@@ -158,6 +160,7 @@ void PVStructureArray::serialize(ByteBuffer *pbuffer,
void PVStructureArray::deserialize(ByteBuffer *pbuffer,
DeserializableControl *pcontrol) {
RecurseGuard G;
svector data(reuse());
size_t size = this->getArray()->getArraySizeType() == Array::fixed ?
+3
View File
@@ -18,6 +18,8 @@
#include <pv/factory.h>
#include <pv/serializeHelper.h>
#include "factoryPvt.h"
using std::tr1::static_pointer_cast;
using std::size_t;
using std::string;
@@ -143,6 +145,7 @@ void PVUnion::serialize(ByteBuffer *pbuffer, SerializableControl *pflusher) cons
void PVUnion::deserialize(ByteBuffer *pbuffer, DeserializableControl *pcontrol)
{
RecurseGuard G;
if (variant)
{
FieldConstPtr field = pcontrol->cachedDeserialize(pbuffer);
+3
View File
@@ -16,6 +16,8 @@
#include <pv/factory.h>
#include <pv/serializeHelper.h>
#include "factoryPvt.h"
using std::tr1::static_pointer_cast;
using std::size_t;
@@ -157,6 +159,7 @@ void PVUnionArray::serialize(ByteBuffer *pbuffer,
void PVUnionArray::deserialize(ByteBuffer *pbuffer,
DeserializableControl *pcontrol) {
RecurseGuard G;
svector data(reuse());
size_t size = this->getArray()->getArraySizeType() == Array::fixed ?
+14
View File
@@ -0,0 +1,14 @@
#ifndef FACTORYPVT_H
#define FACTORYPVT_H
namespace epics { namespace pvData {
// use to bound recursion in FieldCreate and PVDataCreator singletons
struct RecurseGuard {
RecurseGuard();
~RecurseGuard();
};
}} // namespace epics::pvData
#endif // FACTORYPVT_H
+23 -1
View File
@@ -874,13 +874,35 @@ void testMalformed()
// a union array where ...
CASE("\x89\x83\x2b\x01\x01");
#undef CASE
{
std::vector<char> inp(500u);
for(size_t i=0; i<inp.size(); i+=5) {
inp[i+0] = '\x80'; // Struct
inp[i+1] = '\xff'; // null ID String
inp[i+2] = '\x01'; // 1 member field
inp[i+3] = '\x01'; // member name length 1
inp[i+4] = 'x' ; // member name
}
testDeserializeTypeFail(&inp[0], inp.size());
}
{
std::vector<char> inp(500u);
for(size_t i=0; i<inp.size(); i+=5) {
inp[i+0] = '\x81'; // Union
inp[i+1] = '\xff'; // null ID String
inp[i+2] = '\x01'; // 1 member field
inp[i+3] = '\x01'; // member name length 1
inp[i+4] = 'x' ; // member name
}
testDeserializeTypeFail(&inp[0], inp.size());
}
}
} // end namespace
MAIN(testSerialization) {
testPlan(236);
testPlan(238);
flusher = new SerializableControlImpl();
control = new DeserializableControlImpl();