Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: maxfwd module incorrectly decrements header twice if the max-forwards… #3511

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

davidtrihy-genesys
Copy link
Contributor

Summary
Solves an issue where using the maxfwd module function mf_process_maxfwd_header can incorrectly double decrement the header value if sipmsg_validate function with flag h is called before calling the aforementioned function

Details
This is a bug fix which should prevent future issues when using maxfwd which assumes that the parsed value in hdr_field will only ever be used by the maxfwds module which can cause an incorrect double decrement of the header value if some other module was the set the parsed memory to something other than null

Solution
The maxfwd module has two macros defined which store and fetch the value of the parsed property of the maxforwards header in the SIP Message, when storing it stores it adding 1 to the value and when fetching it subtracts 1 from the value. This is because this memory block is a void* and 0 represents a null pointer.

A problem arises when using sipmsg_validate to validate the headers, it will parse the value of the header into the parsed field and when maxfwds checks for the value it assumes it has already been added by the module which adds 1 to the value so when it fetches the value it will then subtract 1 from it causing the double decrement of the value.

The solution is a partial rewrite of the module where the addition and subtraction logic in the macro is removed in favour of assuming a value of 0 and a null value act exactly the same anyway so when the header is parsed and the value is 0 then we can just try and parse it from the body again anyway and end up with a value of 0 again but if the value is greater than 0 then we can just fetch the value in the parsed field and return, the trim logic which gets the string representation is called in all cases before checking if the value exists in the parsed header and/or fetching it. We can assume that if we get to trim stage then the header has a body at this stage

Compatibility
No compatibility issues, in fact it fixes a compatibility issue with sipmsgops and maxfwds

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed the macros from MAXWD to MAXFWD also some of the changes here are whitespace changes to be consistent

return -1;
}
} else if (IS_MAXWD_STORED(msg)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else/if is removed here in favour of an if after this is so that we can do the trimlen in one place just before checking if IS_MAXFWD_STORED which it will be when using sipmsg_validate assuming it is non zero value so we trim and then check and retrieve the value

x = str2s( foo->s,foo->len,&err);
if (err){
LM_ERR("unable to parse the max forwards number\n");
parsed_val = str2s(mf_value->s, mf_value->len, &err);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assume in this scenario that IS_MAXFWD_STORED failed because either we don't have a value in the parsed field because maxfwds module is the first instance of setting this value or that sipmsgops has not validated the SIP headers so we parse the body, we can only get here if the header has been parsed and if it is actually zero we can just return it

@davidtrihy-genesys davidtrihy-genesys changed the title maxfwd module incorrectly decrements header twice if the max-forwards… BUG: maxfwd module incorrectly decrements header twice if the max-forwards… Nov 11, 2024
Copy link

Any updates here? No progress has been made in the last 30 days, marking as stale.

@github-actions github-actions bot added the stale label Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant