This source file includes following definitions.
- sys_ni_syscall
- proc_sel
- sys_setpriority
- sys_getpriority
- sys_profil
- sys_ftime
- sys_break
- sys_stty
- sys_gtty
- sys_prof
- sys_reboot
- ctrl_alt_del
- sys_setregid
- sys_setgid
- sys_acct
- sys_phys
- sys_lock
- sys_mpx
- sys_ulimit
- sys_old_syscall
- sys_setreuid
- sys_setuid
- sys_setfsuid
- sys_setfsgid
- sys_times
- sys_brk
- sys_setpgid
- sys_getpgid
- sys_getpgrp
- sys_setsid
- sys_getgroups
- sys_setgroups
- in_group_p
- sys_newuname
- sys_uname
- sys_olduname
- sys_sethostname
- sys_gethostname
- sys_setdomainname
- sys_getrlimit
- sys_setrlimit
- getrusage
- sys_getrusage
- sys_umask
1
2
3
4
5
6
7 #include <linux/errno.h>
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/times.h>
11 #include <linux/utsname.h>
12 #include <linux/param.h>
13 #include <linux/resource.h>
14 #include <linux/signal.h>
15 #include <linux/string.h>
16 #include <linux/ptrace.h>
17 #include <linux/stat.h>
18 #include <linux/mman.h>
19 #include <linux/mm.h>
20
21 #include <asm/segment.h>
22 #include <asm/io.h>
23
24
25
26
27 static int C_A_D = 1;
28
29 extern void adjust_clock(void);
30
31 #define PZERO 15
32
33 asmlinkage int sys_ni_syscall(void)
34 {
35 return -ENOSYS;
36 }
37
38 static int proc_sel(struct task_struct *p, int which, int who)
39 {
40 switch (which) {
41 case PRIO_PROCESS:
42 if (!who && p == current)
43 return 1;
44 return(p->pid == who);
45 case PRIO_PGRP:
46 if (!who)
47 who = current->pgrp;
48 return(p->pgrp == who);
49 case PRIO_USER:
50 if (!who)
51 who = current->uid;
52 return(p->uid == who);
53 }
54 return 0;
55 }
56
57 asmlinkage int sys_setpriority(int which, int who, int niceval)
58 {
59 struct task_struct **p;
60 int error = ESRCH;
61 int priority;
62
63 if (which > 2 || which < 0)
64 return -EINVAL;
65
66 if ((priority = PZERO - niceval) <= 0)
67 priority = 1;
68
69 for(p = &LAST_TASK; p > &FIRST_TASK; --p) {
70 if (!*p || !proc_sel(*p, which, who))
71 continue;
72 if ((*p)->uid != current->euid &&
73 (*p)->uid != current->uid && !suser()) {
74 error = EPERM;
75 continue;
76 }
77 if (error == ESRCH)
78 error = 0;
79 if (priority > (*p)->priority && !suser())
80 error = EACCES;
81 else
82 (*p)->priority = priority;
83 }
84 return -error;
85 }
86
87 asmlinkage int sys_getpriority(int which, int who)
88 {
89 struct task_struct **p;
90 int max_prio = 0;
91
92 if (which > 2 || which < 0)
93 return -EINVAL;
94
95 for(p = &LAST_TASK; p > &FIRST_TASK; --p) {
96 if (!*p || !proc_sel(*p, which, who))
97 continue;
98 if ((*p)->priority > max_prio)
99 max_prio = (*p)->priority;
100 }
101 return(max_prio ? max_prio : -ESRCH);
102 }
103
104 asmlinkage int sys_profil(void)
105 {
106 return -ENOSYS;
107 }
108
109 asmlinkage int sys_ftime(void)
110 {
111 return -ENOSYS;
112 }
113
114 asmlinkage int sys_break(void)
115 {
116 return -ENOSYS;
117 }
118
119 asmlinkage int sys_stty(void)
120 {
121 return -ENOSYS;
122 }
123
124 asmlinkage int sys_gtty(void)
125 {
126 return -ENOSYS;
127 }
128
129 asmlinkage int sys_prof(void)
130 {
131 return -ENOSYS;
132 }
133
134 extern void hard_reset_now(void);
135 extern asmlinkage sys_kill(int, int);
136
137
138
139
140
141
142
143
144
145 asmlinkage int sys_reboot(int magic, int magic_too, int flag)
146 {
147 if (!suser())
148 return -EPERM;
149 if (magic != 0xfee1dead || magic_too != 672274793)
150 return -EINVAL;
151 if (flag == 0x01234567)
152 hard_reset_now();
153 else if (flag == 0x89ABCDEF)
154 C_A_D = 1;
155 else if (!flag)
156 C_A_D = 0;
157 else if (flag == 0xCDEF0123) {
158 printk(KERN_EMERG "System halted\n");
159 sys_kill(-1, SIGKILL);
160 do_exit(0);
161 } else
162 return -EINVAL;
163 return (0);
164 }
165
166
167
168
169
170
171 void ctrl_alt_del(void)
172 {
173 if (C_A_D)
174 hard_reset_now();
175 else
176 send_sig(SIGINT,task[1],1);
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 asmlinkage int sys_setregid(gid_t rgid, gid_t egid)
196 {
197 int old_rgid = current->gid;
198 int old_egid = current->egid;
199
200 if (rgid != (gid_t) -1) {
201 if ((old_rgid == rgid) ||
202 (current->egid==rgid) ||
203 suser())
204 current->gid = rgid;
205 else
206 return(-EPERM);
207 }
208 if (egid != (gid_t) -1) {
209 if ((old_rgid == egid) ||
210 (current->egid == egid) ||
211 (current->sgid == egid) ||
212 suser())
213 current->egid = egid;
214 else {
215 current->gid = old_rgid;
216 return(-EPERM);
217 }
218 }
219 if (rgid != (gid_t) -1 ||
220 (egid != (gid_t) -1 && egid != old_rgid))
221 current->sgid = current->egid;
222 current->fsgid = current->egid;
223 if (current->egid != old_egid)
224 current->dumpable = 0;
225 return 0;
226 }
227
228
229
230
231 asmlinkage int sys_setgid(gid_t gid)
232 {
233 int old_egid = current->egid;
234
235 if (suser())
236 current->gid = current->egid = current->sgid = current->fsgid = gid;
237 else if ((gid == current->gid) || (gid == current->sgid))
238 current->egid = current->fsgid = gid;
239 else
240 return -EPERM;
241 if (current->egid != old_egid)
242 current->dumpable = 0;
243 return 0;
244 }
245
246 asmlinkage int sys_acct(void)
247 {
248 return -ENOSYS;
249 }
250
251 asmlinkage int sys_phys(void)
252 {
253 return -ENOSYS;
254 }
255
256 asmlinkage int sys_lock(void)
257 {
258 return -ENOSYS;
259 }
260
261 asmlinkage int sys_mpx(void)
262 {
263 return -ENOSYS;
264 }
265
266 asmlinkage int sys_ulimit(void)
267 {
268 return -ENOSYS;
269 }
270
271 asmlinkage int sys_old_syscall(void)
272 {
273 return -ENOSYS;
274 }
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291 asmlinkage int sys_setreuid(uid_t ruid, uid_t euid)
292 {
293 int old_ruid = current->uid;
294 int old_euid = current->euid;
295
296 if (ruid != (uid_t) -1) {
297 if ((old_ruid == ruid) ||
298 (current->euid==ruid) ||
299 suser())
300 current->uid = ruid;
301 else
302 return(-EPERM);
303 }
304 if (euid != (uid_t) -1) {
305 if ((old_ruid == euid) ||
306 (current->euid == euid) ||
307 (current->suid == euid) ||
308 suser())
309 current->euid = euid;
310 else {
311 current->uid = old_ruid;
312 return(-EPERM);
313 }
314 }
315 if (ruid != (uid_t) -1 ||
316 (euid != (uid_t) -1 && euid != old_ruid))
317 current->suid = current->euid;
318 current->fsuid = current->euid;
319 if (current->euid != old_euid)
320 current->dumpable = 0;
321 return 0;
322 }
323
324
325
326
327
328
329
330
331
332
333
334
335 asmlinkage int sys_setuid(uid_t uid)
336 {
337 int old_euid = current->euid;
338
339 if (suser())
340 current->uid = current->euid = current->suid = current->fsuid = uid;
341 else if ((uid == current->uid) || (uid == current->suid))
342 current->fsuid = current->euid = uid;
343 else
344 return -EPERM;
345 if (current->euid != old_euid)
346 current->dumpable = 0;
347 return(0);
348 }
349
350
351
352
353
354
355
356 asmlinkage int sys_setfsuid(uid_t uid)
357 {
358 int old_fsuid = current->fsuid;
359
360 if (uid == current->uid || uid == current->euid ||
361 uid == current->suid || uid == current->fsuid || suser())
362 current->fsuid = uid;
363 if (current->fsuid != old_fsuid)
364 current->dumpable = 0;
365 return old_fsuid;
366 }
367
368
369
370
371 asmlinkage int sys_setfsgid(gid_t gid)
372 {
373 int old_fsgid = current->fsgid;
374
375 if (gid == current->gid || gid == current->egid ||
376 gid == current->sgid || gid == current->fsgid || suser())
377 current->fsgid = gid;
378 if (current->fsgid != old_fsgid)
379 current->dumpable = 0;
380 return old_fsgid;
381 }
382
383 asmlinkage int sys_times(struct tms * tbuf)
384 {
385 if (tbuf) {
386 int error = verify_area(VERIFY_WRITE,tbuf,sizeof *tbuf);
387 if (error)
388 return error;
389 put_user(current->utime,&tbuf->tms_utime);
390 put_user(current->stime,&tbuf->tms_stime);
391 put_user(current->cutime,&tbuf->tms_cutime);
392 put_user(current->cstime,&tbuf->tms_cstime);
393 }
394 return jiffies;
395 }
396
397 asmlinkage unsigned long sys_brk(unsigned long brk)
398 {
399 int freepages;
400 unsigned long rlim;
401 unsigned long newbrk, oldbrk;
402
403 if (brk < current->mm->end_code)
404 return current->mm->brk;
405 newbrk = PAGE_ALIGN(brk);
406 oldbrk = PAGE_ALIGN(current->mm->brk);
407 if (oldbrk == newbrk)
408 return current->mm->brk = brk;
409
410
411
412
413 if (brk <= current->mm->brk) {
414 current->mm->brk = brk;
415 do_munmap(newbrk, oldbrk-newbrk);
416 return brk;
417 }
418
419
420
421 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
422 if (rlim >= RLIM_INFINITY)
423 rlim = ~0;
424 if (brk - current->mm->end_code > rlim)
425 return current->mm->brk;
426
427
428
429 if (find_vma_intersection(current, oldbrk, newbrk+PAGE_SIZE))
430 return current->mm->brk;
431
432
433
434
435
436 freepages = buffermem >> PAGE_SHIFT;
437 freepages += nr_free_pages;
438 freepages += nr_swap_pages;
439 freepages -= MAP_NR(high_memory) >> 4;
440 freepages -= (newbrk-oldbrk) >> PAGE_SHIFT;
441 if (freepages < 0)
442 return current->mm->brk;
443 #if 0
444 freepages += current->mm->rss;
445 freepages -= oldbrk >> 12;
446 if (freepages < 0)
447 return current->mm->brk;
448 #endif
449
450
451
452 current->mm->brk = brk;
453 do_mmap(NULL, oldbrk, newbrk-oldbrk,
454 PROT_READ|PROT_WRITE|PROT_EXEC,
455 MAP_FIXED|MAP_PRIVATE, 0);
456 return brk;
457 }
458
459
460
461
462
463
464
465
466
467
468
469
470
471 asmlinkage int sys_setpgid(pid_t pid, pid_t pgid)
472 {
473 struct task_struct * p;
474
475 if (!pid)
476 pid = current->pid;
477 if (!pgid)
478 pgid = pid;
479 if (pgid < 0)
480 return -EINVAL;
481 for_each_task(p) {
482 if (p->pid == pid)
483 goto found_task;
484 }
485 return -ESRCH;
486
487 found_task:
488 if (p->p_pptr == current || p->p_opptr == current) {
489 if (p->session != current->session)
490 return -EPERM;
491 if (p->did_exec)
492 return -EACCES;
493 } else if (p != current)
494 return -ESRCH;
495 if (p->leader)
496 return -EPERM;
497 if (pgid != pid) {
498 struct task_struct * tmp;
499 for_each_task (tmp) {
500 if (tmp->pgrp == pgid &&
501 tmp->session == current->session)
502 goto ok_pgid;
503 }
504 return -EPERM;
505 }
506
507 ok_pgid:
508 p->pgrp = pgid;
509 return 0;
510 }
511
512 asmlinkage int sys_getpgid(pid_t pid)
513 {
514 struct task_struct * p;
515
516 if (!pid)
517 return current->pgrp;
518 for_each_task(p) {
519 if (p->pid == pid)
520 return p->pgrp;
521 }
522 return -ESRCH;
523 }
524
525 asmlinkage int sys_getpgrp(void)
526 {
527 return current->pgrp;
528 }
529
530 asmlinkage int sys_setsid(void)
531 {
532 if (current->leader)
533 return -EPERM;
534 current->leader = 1;
535 current->session = current->pgrp = current->pid;
536 current->tty = NULL;
537 current->tty_old_pgrp = 0;
538 return current->pgrp;
539 }
540
541
542
543
544 asmlinkage int sys_getgroups(int gidsetsize, gid_t *grouplist)
545 {
546 int i;
547 int * groups;
548
549 if (gidsetsize) {
550 i = verify_area(VERIFY_WRITE, grouplist, sizeof(gid_t) * gidsetsize);
551 if (i)
552 return i;
553 }
554 groups = current->groups;
555 for (i = 0 ; (i < NGROUPS) && (*groups != NOGROUP) ; i++, groups++) {
556 if (!gidsetsize)
557 continue;
558 if (i >= gidsetsize)
559 break;
560 put_user(*groups, grouplist);
561 grouplist++;
562 }
563 return(i);
564 }
565
566 asmlinkage int sys_setgroups(int gidsetsize, gid_t *grouplist)
567 {
568 int i;
569
570 if (!suser())
571 return -EPERM;
572 if (gidsetsize > NGROUPS)
573 return -EINVAL;
574 i = verify_area(VERIFY_READ, grouplist, sizeof(gid_t) * gidsetsize);
575 if (i)
576 return i;
577 for (i = 0; i < gidsetsize; i++, grouplist++) {
578 current->groups[i] = get_user(grouplist);
579 }
580 if (i < NGROUPS)
581 current->groups[i] = NOGROUP;
582 return 0;
583 }
584
585 int in_group_p(gid_t grp)
586 {
587 int i;
588
589 if (grp == current->fsgid)
590 return 1;
591
592 for (i = 0; i < NGROUPS; i++) {
593 if (current->groups[i] == NOGROUP)
594 break;
595 if (current->groups[i] == grp)
596 return 1;
597 }
598 return 0;
599 }
600
601 asmlinkage int sys_newuname(struct new_utsname * name)
602 {
603 int error;
604
605 if (!name)
606 return -EFAULT;
607 error = verify_area(VERIFY_WRITE, name, sizeof *name);
608 if (!error)
609 memcpy_tofs(name,&system_utsname,sizeof *name);
610 return error;
611 }
612
613 asmlinkage int sys_uname(struct old_utsname * name)
614 {
615 int error;
616 if (!name)
617 return -EFAULT;
618 error = verify_area(VERIFY_WRITE, name,sizeof *name);
619 if (error)
620 return error;
621 memcpy_tofs(&name->sysname,&system_utsname.sysname,
622 sizeof (system_utsname.sysname));
623 memcpy_tofs(&name->nodename,&system_utsname.nodename,
624 sizeof (system_utsname.nodename));
625 memcpy_tofs(&name->release,&system_utsname.release,
626 sizeof (system_utsname.release));
627 memcpy_tofs(&name->version,&system_utsname.version,
628 sizeof (system_utsname.version));
629 memcpy_tofs(&name->machine,&system_utsname.machine,
630 sizeof (system_utsname.machine));
631 return 0;
632 }
633
634 asmlinkage int sys_olduname(struct oldold_utsname * name)
635 {
636 int error;
637 if (!name)
638 return -EFAULT;
639 error = verify_area(VERIFY_WRITE, name,sizeof *name);
640 if (error)
641 return error;
642 memcpy_tofs(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
643 put_user(0,name->sysname+__OLD_UTS_LEN);
644 memcpy_tofs(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
645 put_user(0,name->nodename+__OLD_UTS_LEN);
646 memcpy_tofs(&name->release,&system_utsname.release,__OLD_UTS_LEN);
647 put_user(0,name->release+__OLD_UTS_LEN);
648 memcpy_tofs(&name->version,&system_utsname.version,__OLD_UTS_LEN);
649 put_user(0,name->version+__OLD_UTS_LEN);
650 memcpy_tofs(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
651 put_user(0,name->machine+__OLD_UTS_LEN);
652 return 0;
653 }
654
655 asmlinkage int sys_sethostname(char *name, int len)
656 {
657 int error;
658
659 if (!suser())
660 return -EPERM;
661 if (len < 0 || len > __NEW_UTS_LEN)
662 return -EINVAL;
663 error = verify_area(VERIFY_READ, name, len);
664 if (error)
665 return error;
666 memcpy_fromfs(system_utsname.nodename, name, len);
667 system_utsname.nodename[len] = 0;
668 return 0;
669 }
670
671 asmlinkage int sys_gethostname(char *name, int len)
672 {
673 int i;
674
675 if (len < 0)
676 return -EINVAL;
677 i = verify_area(VERIFY_WRITE, name, len);
678 if (i)
679 return i;
680 i = 1+strlen(system_utsname.nodename);
681 if (i > len)
682 i = len;
683 memcpy_tofs(name, system_utsname.nodename, i);
684 return 0;
685 }
686
687
688
689
690
691 asmlinkage int sys_setdomainname(char *name, int len)
692 {
693 int error;
694
695 if (!suser())
696 return -EPERM;
697 if (len > __NEW_UTS_LEN)
698 return -EINVAL;
699 error = verify_area(VERIFY_READ, name, len);
700 if (error)
701 return error;
702 memcpy_fromfs(system_utsname.domainname, name, len);
703 system_utsname.domainname[len] = 0;
704 return 0;
705 }
706
707 asmlinkage int sys_getrlimit(unsigned int resource, struct rlimit *rlim)
708 {
709 int error;
710
711 if (resource >= RLIM_NLIMITS)
712 return -EINVAL;
713 error = verify_area(VERIFY_WRITE,rlim,sizeof *rlim);
714 if (error)
715 return error;
716 memcpy_tofs(rlim, current->rlim + resource, sizeof(*rlim));
717 return 0;
718 }
719
720 asmlinkage int sys_setrlimit(unsigned int resource, struct rlimit *rlim)
721 {
722 struct rlimit new_rlim, *old_rlim;
723 int err;
724
725 if (resource >= RLIM_NLIMITS)
726 return -EINVAL;
727 err = verify_area(VERIFY_READ, rlim, sizeof(*rlim));
728 if (err)
729 return err;
730 memcpy_fromfs(&new_rlim, rlim, sizeof(*rlim));
731 old_rlim = current->rlim + resource;
732 if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
733 (new_rlim.rlim_max > old_rlim->rlim_max)) &&
734 !suser())
735 return -EPERM;
736 if (resource == RLIMIT_NOFILE) {
737 if (new_rlim.rlim_cur > NR_OPEN || new_rlim.rlim_max > NR_OPEN)
738 return -EPERM;
739 }
740 *old_rlim = new_rlim;
741 return 0;
742 }
743
744
745
746
747
748
749
750
751
752 int getrusage(struct task_struct *p, int who, struct rusage *ru)
753 {
754 int error;
755 struct rusage r;
756
757 error = verify_area(VERIFY_WRITE, ru, sizeof *ru);
758 if (error)
759 return error;
760 memset((char *) &r, 0, sizeof(r));
761 switch (who) {
762 case RUSAGE_SELF:
763 r.ru_utime.tv_sec = CT_TO_SECS(p->utime);
764 r.ru_utime.tv_usec = CT_TO_USECS(p->utime);
765 r.ru_stime.tv_sec = CT_TO_SECS(p->stime);
766 r.ru_stime.tv_usec = CT_TO_USECS(p->stime);
767 r.ru_minflt = p->mm->min_flt;
768 r.ru_majflt = p->mm->maj_flt;
769 break;
770 case RUSAGE_CHILDREN:
771 r.ru_utime.tv_sec = CT_TO_SECS(p->cutime);
772 r.ru_utime.tv_usec = CT_TO_USECS(p->cutime);
773 r.ru_stime.tv_sec = CT_TO_SECS(p->cstime);
774 r.ru_stime.tv_usec = CT_TO_USECS(p->cstime);
775 r.ru_minflt = p->mm->cmin_flt;
776 r.ru_majflt = p->mm->cmaj_flt;
777 break;
778 default:
779 r.ru_utime.tv_sec = CT_TO_SECS(p->utime + p->cutime);
780 r.ru_utime.tv_usec = CT_TO_USECS(p->utime + p->cutime);
781 r.ru_stime.tv_sec = CT_TO_SECS(p->stime + p->cstime);
782 r.ru_stime.tv_usec = CT_TO_USECS(p->stime + p->cstime);
783 r.ru_minflt = p->mm->min_flt + p->mm->cmin_flt;
784 r.ru_majflt = p->mm->maj_flt + p->mm->cmaj_flt;
785 break;
786 }
787 memcpy_tofs(ru, &r, sizeof(r));
788 return 0;
789 }
790
791 asmlinkage int sys_getrusage(int who, struct rusage *ru)
792 {
793 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
794 return -EINVAL;
795 return getrusage(current, who, ru);
796 }
797
798 asmlinkage int sys_umask(int mask)
799 {
800 int old = current->fs->umask;
801
802 current->fs->umask = mask & S_IRWXUGO;
803 return (old);
804 }